简体   繁体   English

C#EF向客户表添加记录,其中包含Zip表的外键

[英]C# EF Adding Record to Customer Table which contains foreign key to Zip Table

I am new to entity framework and started developing first application using DB first approach , I have added all records in ZipTable and now struggling to add records in CustomerTable after reading data from .csv file. 我是实体框架的新手,并开始使用数据库优先方法开发第一个应用程序,我已经在ZipTable中添加了所有记录,现在在从.csv文件中读取数据后,很难在CustomerTable中添加记录。

CustomerTable( Phone[PK],FirstName,LastName,Address,Zip[FK] ) CustomerTable( 电话[PK],名字,姓氏,地址,邮政编码[FK]
ZipTable( Zip[PK],City,County ) ZipTable( Zip [PK],City,County

//Class Automatically Generated by EF 

public partial class dbzipcode
{
public dbzipcode()
{
    this.dbcustomers = new HashSet<dbcustomer>();
}

    public string ZIP { get; set; }
    public string CITY { get; set; }
    public string COUNTY { get; set; }

    public virtual ICollection<dbcustomer> dbcustomers { get; set; }
}

public partial class dbcustomer
{
    public string PHONE { get; set; }
    public string FIRSTNAME { get; set; }
    public string LASTNAME { get; set; }
    public string ADDRESS { get; set; }

    public string ZIP { get; set; }
    public virtual dbzipcode dbzipcode { get; set; }
}

I already have the data in DB for ZipCodes Now when i try to add data in CustomerTable i face forignkey error, I want that in Customer zip should be inserted if corresponding Zip exists in secondry table otherwise it should throw exception. 我已经在ZipCodes的DB中拥有数据了。现在,当我尝试在CustomerTable中添加数据时,我遇到了Forignkey错误,如果要在Secondry表中存在相应的Zip,我希望在Customer zip中插入,否则应该抛出异常。

To acheive this i am first reading Zip data from csv before inserting each record. 为此,我首先要在插入每个记录之前从csv中读取Zip数据。 I believe this is not the right way, can someone point me in the right direction ? 我认为这不是正确的方法,有人可以指出正确的方向吗?

                    dbcustomer customer = new dbcustomer();
                    customer.PHONE = splits[columnIndexPhoneNumber];
                    customer.FIRSTNAME = splits[columnIndexFirstName];
                    customer.LASTNAME = splits[columnIndexLastName];
                    customer.ADDRESS = splits[columnIndexAddress];
                    customer.ZIP = splits[columnIndexZipCode];

                    //dbzipcode z = new dbzipcode() { ZIP=customer.ZIP};

                    dbzipcode z = ZipCodeDbHandler.GetInstance().GetZipCodeFromDb(splits[columnIndexZipCode]);
                    customer.dbzipcode = z;

you have customer table , and zip table , 您有客户表和邮编表,

i'm going to record new customer has zip code not found in my zip code table , so you set general zip or none , or make the ability to add the new zip code before complete record , its ok no wrong way ( it will be wrong if the has complete zip code in zip table and customer can not be outside of this ) , so option : record in customer table and if zip code recorded not in the zip table then do exception and ask to confirm record zip code , or alert must enter valid zip code 我要记录新客户的邮政编码表中未找到邮政编码,因此您可以设置常规邮政编码或不设置邮政编码,或者可以在完成记录之前添加新的邮政编码,这没错,这是正确的方法(如果邮政编码表中有完整的邮政编码并且客户不能不在此范围内,则是错误的,因此选项:在客户表中记录,如果邮政编码中没有记录邮政编码,则进行异常并要求确认记录邮政编码,或警告必须输入有效的邮政编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM