1

我需要一些帮助,我一直在到处寻找为什么这种情况不断发生。我基本上有 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)
);

课程

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"));

我一直在使用 Code First,并且数据库已经存在。我从数据库中得到的结果是:

公司表:

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

我对控制器的看法是:

    [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>

任何帮助请......

4

1 回答 1

0

您可能不正确地保存数据。您是否尝试过将您的地址数据添加到您的公司模型中,然后只保存公司:

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

希望有帮助。

于 2012-12-20T14:29:51.807 回答