一对多实体框架更新不及时的外键(One to Many with Entity Framework

2019-10-17 12:03发布

我需要一些帮助,我一直在到处寻找的年龄为什么持续发生。 我基本上有2个表,公司和地址。 一个公司可以有多个地址。 我的类和表如下所示

表:

create table Company (
    companyid int PRIMARY KEY IDENTITY not null,
    name varchar(100),
    agencyref varchar(20),
    website varchar(100),
    phoneno varchar(20),
    faxno varchar(20),
    modified datetime DEFAULT (GETDATE())   
);

create table Address (
      addressid int PRIMARY KEY IDENTITY not null,
      companyid int null, //Does the same if set to not null
        addressLine1 varchar(100),
        addressLine2 varchar(100),
        city varchar(50),
        county varchar(100),
        postcode varchar(10),
        modified datetime DEFAULT (GETDATE()),
        foreign key ( companyid ) references Company (companyid)
);

CLASSSES

public class Company
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int companyid { get; set; }

    [DisplayName("Company Name:")]
    public string name { get; set; }

    [DisplayName("Client Reference:")]
    public string agencyref { get; set; }

    [DisplayName("Website Address:")]
    public string website { get; set; }

    [DisplayName("Phone No:")]
    public string phoneno { get; set; }

    [DisplayName("Fax No:")]
    public string faxno { get; set; }

    private DateTime _modified = DateTime.Now;
    [DataType(DataType.DateTime)]
    public DateTime modified
    {
        get { return _modified; }
        set { _modified = value; }
    }
    public virtual ICollection<Address> Address { get; set; }
}

public class Address
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int addressid { get; set; }

    [DisplayName("Address Line 1:")]
    public string addressline1 { get; set; }

    [DisplayName("Address Line 2:")]
    public string addressline2 { get; set; }

    [DisplayName("City:")]
    public string city { get; set; }

    [DisplayName("County:")]
    public string county { get; set; }

    [DisplayName("Postcode:")]
    public string postcode { get; set; }

    private DateTime _modified = DateTime.Now;

    [DataType(DataType.DateTime)]
    public DateTime modified
    {
        get { return _modified; }
        set { _modified = value; }
    }
    public virtual Company Company { get; set; }
}

我的模型构建

modelBuilder.Entity<Company>().HasMany(c => c.Address).WithOptional(p => p.Company).Map(c => c.MapKey("companyid"));

我一直在使用代码第一次和数据库已经存在。 结果我从数据库中获取的是:

运营商表:

companyid   name    agencyref   website phoneno faxno   modified
4   erg NULL    NULL    NULL    NULL    2012-12-20 13:20:56.430

地址表:

addressid   companyid   addressLine1    addressLine2    city    county  postcode    modified
2   NULL    hkjh    NULL    NULL    NULL    NULL    2012-12-20 13:50:37.977

正如你可以看到我期望在地址表中的companyid与4被填充,但由于某种原因,这是不这样做。

不知道是否有人能帮助我解决这个令人沮丧的问题。 我认为这是简单的使用连接到一个视图模式,即返回的数据填充两班,然后插入到这个数据库中的观点:

我clientModel类是好的和简单的:

public class ClientModel
{
    public Company Company { get; set; }
    public Address Address { get; set; }
}

MY控制器上的看法是:

    [HttpPost]
    public ActionResult Edit(ClientModel client)
    {
       // CompanyAddress ca = new CompanyAddress();
        Domain.Data.EFDbContext context = new Domain.Data.EFDbContext();
        if (ModelState.IsValid)
        {
            context.Companies.Add(client.Company);
            context.Addresses.Add(client.Address);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            return View(client);
        }
    }

这就是让我的编辑页面包含

@model TimesheetMVC.WebUI.Models.ClientModel

(一个FormMethod.Post内封闭的)

<fieldset>
    <legend>Company Details</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Company.name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Company.name)
        @Html.ValidationMessageFor(model => model.Company.name)
    </div>        
    <div class="editor-label">
        @Html.LabelFor(model => model.Company.agencyref)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Company.agencyref)
        @Html.ValidationMessageFor(model => model.Company.agencyref)
    </div>

</fieldset>
<fieldset>
    <legend>Address Details</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Address.addressline1)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.addressline1)
        @Html.ValidationMessageFor(model => model.Address.addressline1)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address.addressline2)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.addressline2)
        @Html.ValidationMessageFor(model => model.Address.addressline2)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address.city)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.city)
        @Html.ValidationMessageFor(model => model.Address.city)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address.county)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.county)
        @Html.ValidationMessageFor(model => model.Address.county)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Address.postcode)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address.postcode)
        @Html.ValidationMessageFor(model => model.Address.postcode)
    </div>

任何帮助,请......

Answer 1:

您可能会错误地保存数据。 您是否尝试过加入您的地址信息到你的公司模式,那么,拯救公司:

Domain.Data.EFDbContext context = new Domain.Data.EFDbContext();
if (ModelState.IsValid)
{
    client.Company.Address.Add(client.Address);
    context.Companies.Add(client.Company);
    context.SaveChanges();
    return RedirectToAction("Index");
}
else
{
    return View(client);
}

希望帮助。



文章来源: One to Many with Entity Framework not updating the foreign key