【发布时间】:2014-09-23 08:40:40
【问题描述】:
我正在做一个通过 EF 将实体插入 DB 的方法。
我的问题是我需要向这些实体插入地址,但我无法让它工作。
地址模型:
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到Address(AddressId)
con.Address = add;
我已经试过了
con.AddressId = add.AddressId
但它似乎不起作用...... 它不断给我“地址 ID 字段是必需的”。因为 Id 为 0。似乎 EF 外键不起作用。
有什么想法吗?
【问题讨论】:
-
您缺少从
Address到AddressConnection的导航属性。 -
@DavidG 真的需要吗?我只在一个项目中使用了导航属性,但后来我发现没有它它仍然可以工作......我会试一试
-
@DavidG 哦,我有一个类似的方法正在工作,没有导航属性
-
@DavidG 好的,即使使用导航属性它也不起作用...
标签: asp.net-mvc asp.net-mvc-5 entity-framework-6