2014-09-23 56 views
0

我正在做一個通過EF插入實體到DB的方法。MVC5 - EF6 - 需要<Id>字段

我的問題是,我需要插入地址到這些實體,我無法讓它工作。

地址型號:

public class Address 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int AddressId { get; set; } 

    [Required] 
    [MaxLength(300)] 
    public string AddressName { get; set; } 

    [MaxLength(30)] 
    public string PhoneAddress { get; set; } 

    [MaxLength(30)] 
    public string FaxAddress { get; set; } 

    [MaxLength(50)] 
    public string CountryCode { get; set; } 

    [MaxLength(150)] 
    public string Local { get; set; } 

    [MaxLength(150)] 
    public string PostalCode { get; set; } 

    [ForeignKey("CountryCode")] 
    public virtual Country Country { get; set; } 
} 

地址連接型號:

public class AddressConnection 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int ID { get; set; } 

    [Required] 
    public int AddressId { get; set; } 

    [MaxLength(50)] 
    public string SROCID { get; set; } 

    [MaxLength(50)] 
    public string ROCID { get; set; } 

    public int? EntityID { get; set; } 

    [ForeignKey("AddressId")] 
    public virtual Address Address { get; set; } 

    [ForeignKey("SROCID")] 
    public virtual SROC SROC { get; set; } 

    [ForeignKey("ROCID")] 
    public virtual ROC ROC { get; set; } 

    [ForeignKey("EntityID")] 
    public virtual Entity Entity { get; set; } 
} 

計劃:

#region Address 

string AddressName = _item.address1_line1Field; 
string PostalCode = _item.address1_postalcodeField; 
string CountryCode = _item.address1_countryField != null ? _item.address1_countryField : null; 
string Local = _item.address1_cityField; 
int ID = model.ID; 
string Phone = _item.address1_telephone1Field; 
string Fax = _item.address1_faxField; 
bool added = true; 

if (string.IsNullOrEmpty(AddressName) || string.IsNullOrEmpty(PostalCode) || string.IsNullOrEmpty(Local)) 
    added = false; 

if (added) 
{ 
    Address add = db.Address.FirstOrDefault(x => x.AddressName == AddressName && x.PostalCode == PostalCode && x.CountryCode == CountryCode && x.Local == Local); 

    if (add == null) 
    { 
     add = new Address(); 

     add.AddressName = AddressName; 
     add.CountryCode = CountryCode; 
     add.Local = Local; 
     add.PostalCode = PostalCode; 
     add.PhoneAddress = Phone; 
     add.FaxAddress = Fax; 
     db.Entry<Address>(add).State = EntityState.Added; 
    } 
    else 
    { 
     add.PhoneAddress = Phone; 
     add.FaxAddress = Fax; 
     db.Entry<Address>(add).State = EntityState.Modified; 
    } 

    AddressConnection con = db.AddressConnection.FirstOrDefault(x => x.AddressId == add.AddressId && 
     x.EntityID == ID); 

    if (con == null) 
    { 
     con = new AddressConnection(); 

     con.Address = add; 
     con.EntityID = model.ID; 

     db.Entry<AddressConnection>(con).State = EntityState.Added; 
    } 

    if (add != null) 
    { 
     AddressConnection con2 = db.AddressConnection.FirstOrDefault(x => x.EntityID == ID && x.AddressId != add.AddressId); 
     if (con2 != null) 
     { 
      db.Entry<AddressConnection>(con2).State = EntityState.Deleted; 
     } 
    } 
} 

if (!added) 
    LogExtensions.InsertQueueLog("SyncESR", "NO ADDRESS AT Item:" + ESRId, "NOADDRESS", Membership.CurrentUserId, EntryNo); 

#endregion 

db.SaveChanges(); 

的問題是AddressConnection(CON)需要一個外鍵ID到地址(AddressId)

con.Address = add; 

我已經嘗試過

con.AddressId = add.AddressId 

,但它似乎沒有工作... 它不斷給我了「AddressId字段是必需的。」因爲Id是0.看起來EF外鍵不起作用。

有沒有想法?

+0

您缺少從「地址」到「AddressConnection」的導航屬性。 – DavidG 2014-09-23 09:01:30

+0

@DavidG真的需要嗎?我只在一個項目中使用導航屬性,但後來我發現它在沒有它的情況下一直工作...我會試一試 – 2014-09-23 09:03:18

+0

@DavidG哦,我有一個類似的方法,正在工作,沒有導航屬性 – 2014-09-23 09:04:39

回答

0

OK,不知何故EF並沒有映射在遷移外鍵...

我刪除從AddressConnection型號外鍵屬性和更新的數據庫,然後我加入了外鍵模型並更新,它開始工作。