MVC5 - EF6 - The field is required

asp.net-mvc asp.net-mvc-5 entity-framework-6

Question

I'm doing a method that inserts entities into DB via EF.

My problem is that i need to insert addresses to those entities and i can't get it to work.

Address Model:

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; }
}

Address Connection Model:

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; }
}

Program:

#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();

The problem is that the AddressConnection (con) needs a foreign key id to Address (AddressId)

con.Address = add;

i already tried

con.AddressId = add.AddressId

but it doesn't seem to work... It keeps giving me the "The AddressId field is required." because the Id is 0. It seems that EF foreign key isn't working.

Any thought?

1
0
9/23/2014 8:40:40 AM

Accepted Answer

OK, somehow the EF didn't map the foreign key in the migration...

I removed the foreign key property from the AddressConnection Model and updated the database and then i added the foreign key to the model and updated, it started working.

0
9/23/2014 9:52:22 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow