MVC5 - EF6 - <id>字段是必需的</id>

时间:2014-09-23 08:40:41

标签: asp.net-mvc asp.net-mvc-5 entity-framework-6

我正在做一个通过EF将实体插入数据库的方法。

我的问题是我需要向这些实体插入地址,而我无法让它工作。

地址模型:

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

但它似乎没有效果...... 它一直给我#34; AddressId字段是必需的。&#34;因为Id为0.看来EF外键不起作用。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的,不知何故,EF没有在迁移中映射外键......

我从AddressConnection模型中删除了外键属性并更新了数据库,然后我将外键添加到模型并更新,它开始工作。