[英]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: 我正在尝试使用实体框架更新数据库,我使用automapper将我的实体映射到viewmodels,并以相同的方式将其映射回来:
[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);
}
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? 当我将其映射回来时,公司实体内的外键CVid变为0,我认为在映射过程中丢失了一些东西,你如何映射外键?
Here is my view model and entity: 这是我的视图模型和实体:
View Model: 查看型号:
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; }
}
Entity: 实体:
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: 这是我尝试更新时的错误消息:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. 操作失败:无法更改关系,因为一个或多个外键属性不可为空。 When a change is made to a relationship, the related foreign-key property is set to a null value. 当对关系进行更改时,相关的外键属性将设置为空值。
I see where the problem is, but don't know how to tell automapper to retain foreign key value 我看到问题所在,但不知道如何告诉automapper保留外键值
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. MyCompanyViewModel
类不包含CVId
属性的定义 ,因此默认情况下, CVId
不知道应该将值注入Company
的CVId
属性。 Just define it: 只需定义它:
public class MyCompanyViewModel
{
public int CVId { get; set; }
// Other properties
}
Then for each CompanyViewModel
add corresponding hidden input field into the view: 然后为每个CompanyViewModel
添加相应的隐藏输入字段到视图中:
@for (int i = 0; i < Model.Companies.Count; i++)
{
// ...
@Html.HiddenFor(m => Model.Companies[i].CVId)
// ...
}
and you are good to go! 你很高兴去!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.