Automapper实体框架外键为null [英] Automapper entity framework foreign key is null
问题描述
我正在尝试使用实体框架更新数据库,我使用自动映射器将我的实体映射到视图模型,并以相同的方式将其映射回去:
I am trying to update the database using entity framework, I map my entities to viewmodels using automapper, and map it back the same way:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([FromJson] MyCVViewModel model)
{
var userId = User.Identity.GetUserId();
//find the cv
CV cv = repository.FindCV(model.CVId);
//auto mapper mapping
Mapper.CreateMap<MyCVViewModel, CV>();
Mapper.CreateMap<MyCompanyViewModel, Company>();
cv = Mapper.Map<MyCVViewModel, CV>(model, cv);
//edit
repository.EditCV(cv);
}
当我将其映射回时,公司实体内部的外键CVid变为0,我认为在映射过程中丢失了一些东西,如何映射外键?
When I map it back, the foreign key CVid inside company entity becomes 0, i think something was lost during the mapping process, how do you map the foreign key?
这是我的视图模型和实体:
Here is my view model and entity:
查看模型:
public class MyCVViewModel
{
public int CVId { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Title cannot exceed 100 characters.")]
[Display(Name = "Title")]
public string Title { get; set; }
[Required]
[StringLength(1000, ErrorMessage = "Statment cannot exceed 1000 characters.")]
[Display(Name = "Statement")]
public string Statement { get; set; }
public bool Reference { get; set; }
public List<MyCompanyViewModel> Companies { get; set; }
}
public class MyCompanyViewModel
{
[Required]
[StringLength(100, ErrorMessage = "Company Name cannot exceed 100 characters.")]
[Display(Name = "Company Name")]
public string CompanyName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Job Title cannot exceed 100 characters.")]
[Display(Name = "Job Title")]
public string JobTitle { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "Start Date")]
public DateTime StartDate { get; set; }
[Required]
[DataType(DataType.Date)]
[Display(Name = "End Date")]
public DateTime EndDate { get; set; }
[Required]
[StringLength(1000, ErrorMessage = "Job Description cannot exceed 1000 characters.")]
[Display(Name = "Job Description")]
public string Description { get; set; }
}
实体:
public class CV
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CVId { get; set; }
public string Title { get; set; }
public string Statement { get; set; }
public bool Reference { get; set; }
public virtual ICollection<Company> Companies { get; set; }
}
public class Company
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public string JobTitle { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string Description { get; set; }
public virtual CV CV { get; set; }
public int CVId { get; set; }
}
这是我尝试更新时的错误消息:
and this is the error message when I try to update:
操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系.对关系进行更改时,相关的外键属性将设置为空值.
我知道问题出在哪里,但不知道如何告诉自动映射器保留外键值
I see where the problem is, but don't know how to tell automapper to retain foreign key value
推荐答案
MyCompanyViewModel
类不包含CVId
属性的定义,因此默认情况下,Automapper不知道他应该带哪个位置用于注入Company
的CVId
属性的值.只需定义它即可:
MyCompanyViewModel
class does not contain a definition of CVId
property, so by default Automapper does not know where he should take a value for injecting into Company
's CVId
property. Just define it:
public class MyCompanyViewModel
{
public int CVId { get; set; }
// Other properties
}
然后为每个CompanyViewModel
在视图中添加相应的隐藏输入字段:
Then for each CompanyViewModel
add corresponding hidden input field into the view:
@for (int i = 0; i < Model.Companies.Count; i++)
{
// ...
@Html.HiddenFor(m => Model.Companies[i].CVId)
// ...
}
你很好!
这篇关于Automapper实体框架外键为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!