我正在使用MVC5和EF6 Code First创建Company
可以包含许多Contact
实体的新实体。但是,只有Company
记录正在被写入数据库。
楷模:
public class Company
{
public virtual int CompanyId { get; set; }
[Required]
public virtual string Name { get; set; }
[Required]
public virtual Address Address { get; set; }
[Required]
public virtual string Phone { get; set; }
public virtual string Email { get; set; }
// can have many Contacts
public virtual IEnumerable<Contact> Contacts { get; set; }
}
public class Contact
{
public virtual int ContactId { get; set; }
[Required]
public virtual string Title { get; set; }
[Required]
public virtual string Forename { get; set; }
[Required]
public virtual string Surname { get; set; }
[Required]
public virtual string Phone { get; set; }
public virtual string Email { get; set; }
// belongs to one Company
public virtual Company Company { get; set; }
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create_POST([Bind(Include = "CallerType,Name,Address,Phone,Email,Contacts")] CompanyViewModel viewModel)
{
if (ModelState.IsValid)
{
// tried commented code to make sure it wasn't a problem with my repository and contact records aren't saved to database...
//var context = new EfDbContext();
//var company = new Company
//{
// Name = viewModel.Name,
// Phone = viewModel.Phone,
// Address = viewModel.Address,
// Email = viewModel.Email
//};
//context.Companies.Add(company);
//var contact = new Contact
//{
// Company = company,
// Title = "mr",
// Forename= "f",
// Surname = "s",
// Phone = "132"
//};
//var contact2 = new Contact
//{
// Company = company,
// Title = "mr",
// Forename = "for",
// Surname = "sur",
// Phone = "987"
//};
//var contacts = new List<Contact> {contact, contact2};
//company.Contacts = contacts;
//context.SaveChanges();
var contacts = viewModel.Contacts.Select(c => new Contact
{
Title = c.Title,
Forename = c.Forename,
Surname = c.Surname,
Phone = c.Phone,
Email = c.Email
});
var company = new Company
{
Name = viewModel.Name,
Phone = viewModel.Phone,
Address = viewModel.Address,
Email = viewModel.Email,
Contacts = contacts
};
await _companyRepository.CreateAsync(company);
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Create", "Enquiry", new { id = company.CompanyId, callerType = viewModel.CallerType });
return Json(new { Url = redirectUrl });
}
Response.StatusCode = 400;
return PartialView("~/Views/Company/_Create.cshtml", viewModel);
}
仓库:
public async Task<TEntity> CreateAsync(TEntity entity)
{
if (entity == null) throw new ArgumentNullException(nameof(entity));
DbContext.Set<TEntity>().Add(entity);
await DbContext.SaveChangesAsync();
return entity;
}
我的印象是,DbContext
将“知道”由于Contacts
填充了它的集合,因此它将创建Contact
链接到created的记录Company
。还是我必须创建一个Contact
表,因为这是其中包含外键的表,EF会“知道”创建第Company
一个表?
还是有必要先实际使用保存Company
实体SaveChanges()
,然后将其分配给Contact
记录并执行第二个记录SaveChanges()
?
如果是这种情况,那么我需要使用Database.BeginTransaction()
吗?
关于此的任何指导都将是很棒的,因为这是我第一次使用Entity Framework。
Contacts
必须是一个ICollection
。EF不支持IEnumerable
作为导航属性,因为无法向其中添加项目。
这也意味着您应该更改创建的代码contacts
:
var contacts = viewModel.Contacts.Select(c => new Contact
{
Title = c.Title,
Forename = c.Forename,
Surname = c.Surname,
Phone = c.Phone,
Email = c.Email
}).ToList(); // <= ToList() added
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句