- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 Entity Framework 并让 Automapper 从我的契约(Contract)中更新我的实体。
我的代码是这样的:
var temp = OrderContract;
Order order = dataAccess.FindOne<Order>(x => x.OrderId == temp.OrderId)
?? new Order();
Mapper.Map(OrderContract, order);
if (order.OrderId <= 0)
dataAccess.Add(order);
(注意:我正在使用存储库模式。dataAccess.FindOne 调用 CreateQuery 以返回一个实体。)
我遇到的问题是人际关系。我在进行更新时遇到此错误(插入工作正常):
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. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
我猜 automapper 没有按照我想要的方式更新。根据错误消息和谷歌搜索,我推测我的集合关系(甚至可能不是集合的关系)正在由 Automapper 重新创建。
我如何告诉 Automapper 只更新而不重新制作任何对象或集合?
猜测:
我读到也许我需要为自动映射器使用 UseDestinationValue
选项。我回去把它放在我所有的收藏中,但是当我这样做时,我的插入会因外键违规而失败。
代码映射:
仅在一个集合上使用 UseDestinationValue(这个插入但不会更新):
//+ Source
Mapper.CreateMap<SourceContract, Source>()
.IgnoreAllNonExisting();
//+ SelectedRequirement
Mapper.CreateMap<SelectedRequirementContract, SelectedRequirement>()
.ForMember(x => x.SelectedRequirementId, opt => opt.MapFrom(src => src.RequirementId))
.IgnoreAllNonExisting();
//+ Comment Contract
Mapper.CreateMap<CommentContract, Comment>()
.ForMember(x => x.CommentText, opt => opt.MapFrom(src => src.Comment))
.IgnoreAllNonExisting();
//+ Order Automapper setup
Mapper.CreateMap<OrderContract, Order>()
.ForMember(x => x.Source, opt => opt.MapFrom(src => src.Source))
.ForMember(x => x.Comment, opt => opt.MapFrom(src => src.Comment))
//Although a mapping was created for Comment entity,
//we still need to map the CommentId of the Order entity otherwise it will remain null during an update.
//Another way to handle this would be to Delete CommentId from the Order entity.
//However, if anyone updates (Update from model) OrderDataModel.edmx that property would show up again thus causing
//a null value to be inserted during an update.
.ForMember(x => x.CommentId, opt => opt.MapFrom(src => src.Comment.CommentId))
.ForMember(x => x.SelectedRequirements, opt => {opt.UseDestinationValue(); opt.MapFrom(src => src.Requirements);})
.ForMember(x => x.OrderStateId, opt => opt.MapFrom(src => src.StateId))
.ForMember(x => x.OrderStateId, opt => opt.MapFrom(src => src.StateId))
.IgnoreAllNonExisting();
到处使用 UseDestinationValue(这个不插入):
//+ Source
Mapper.CreateMap<SourceContract, Source>()
.IgnoreAllNonExisting();
//+ SelectedRequirement
Mapper.CreateMap<SelectedRequirementContract, SelectedRequirement>()
.ForMember(x => x.SelectedRequirementId, opt => { opt.UseDestinationValue(); opt.MapFrom(src => src.RequirementId); })
.IgnoreAllNonExisting();
//+ Comment Contract
Mapper.CreateMap<CommentContract, Comment>()
.ForMember(x => x.CommentText, opt => { opt.UseDestinationValue(); opt.MapFrom(src => src.Comment); })
.IgnoreAllNonExisting();
//+ Order Automapper setup
Mapper.CreateMap<OrderContract, Order>()
.ForMember(x => x.Source, opt => { opt.UseDestinationValue(); opt.MapFrom(src => src.Source); })
.ForMember(x => x.Comment, opt => { opt.UseDestinationValue(); opt.MapFrom(src => src.Comment); })
//Although a mapping was created for Comment entity,
//we still need to map the CommentId of the Order entity otherwise it will remain null during an update.
//Another way to handle this would be to Delete CommentId from the Order entity.
//However, if anyone updates (Update from model) OrderDataModel.edmx that property would show up again thus causing
//a null value to be inserted during an update.
.ForMember(x => x.CommentId, opt => { opt.UseDestinationValue(); opt.MapFrom(src => src.Comment.CommentId); })
.ForMember(x => x.SelectedRequirements, opt => { opt.UseDestinationValue(); opt.MapFrom(src => src.Requirements); })
.ForMember(x => x.OrderStateId, opt => { opt.UseDestinationValue(); opt.MapFrom(src => src.StateId); })
.ForMember(x => x.OrderStateId, opt => { opt.UseDestinationValue(); opt.MapFrom(src => src.StateId); })
.IgnoreAllNonExisting();
我需要什么才能插入和更新?
最佳答案
我认为您想要的是让 AutoMapper 不创建您的 EF 实体并获取您发送的实体。在 2.0 版的自动映射器中,有一个映射方法的重载,您基本上可以在其中传递数据上下文委托(delegate)并让 EF 创建您的对象。
请看这个article .
关于c# - 使用 automapper 更新 Entity Framework 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7810252/
我正在尝试在两个对象列表之间进行映射。源类型具有类型为 A 的复杂属性。 ;目标类型是类型 A 的扁平子集加上源类型中的附加标量属性。 public class A { public int
Automapper 是否可以将平面对象映射到复杂的对象图? Mapper.CreateMap() 将 PersonDto.BirthCertificateFatherName 映射到 Person
我有类似 this. 的问题但是这个答案对我不起作用。 我正在做一个小项目。我有两个域模型(Post,Source): public class Post { public Post()
假设我在“消息”类上有一个“评论”属性。我还有 2 个类属性,它们具有“Body”属性。如果该类设置了任一类属性,我希望 AutoMapper 将 Body 属性投影到模型的 comment 属性中,
我是 AutoMapper 的新手,有一个我正在尝试解决的问题。 如果我有这样的源类: public class Membership { public int MembershipId {
我有一个场景,我想忽略基类中定义的类的某些属性。 我有一个这样的初始映射 Mapper.CreateMap() .Include()
如何覆盖 AutoMapper 用于给定属性的类型转换器? 例如,如果我有: public class Foo { public string Name { get; set; } } pub
我正在尝试使用 AutoMapper 将三个实体模型映射到一个 View 模型中。最终输出应该是一个递归类别树,其中包含类别中的产品。类别树正在运行,但 View 模型的 Products 属性为 n
我有一个 Student目的: public class Student { public int Id { get; set; } public string FirstName {
我有一个复杂的对象,如: public class BusinessUnit { public TradingDesk TradingDesk { get; } pub
假设我有这个类: public class Account { public int AccountID { get; set; } public Enterprise Enterpr
从概念上我发现 Automapper 非常有趣。然而,我正试图在上面烧伤(或加热)我的手指。有人可以帮我开始吗?我还不明白我可以从哪里开始。我会喜欢从头开始写一些代码(而不是使用其他人的样本)然后去做
我一直在尝试创建一个自动映射器自定义值解析器,但我似乎错过了一些设置步骤,因为它似乎永远找不到 public abstract class ValueResolver : IValueResolve
我正在尝试让 AutoMapper 为我们在 View 模型上本地化所有 DateTime 属性。我们在系统中的任何地方都使用 UTC 并将所有内容以 UTC 存储在数据库中,但我们希望自动将其转换为
我知道是 AutoMapper而不是 AutoMerge(r),而是…… 我已经开始使用 AutoMapper 并且需要映射 A -> B,并从 C 添加一些属性,以便 B 成为一种 A + C 的平
鉴于这些类,我如何映射它们的字典? public class TestClass { public string Name { get; set; } } public class TestC
我想自定义 AutoMapper 转换类型的方式,而不丢失 AutoMapper 已实现的功能。 我可以创建一个自定义 ITypeConverter实例,但我不知道如何调用默认行为。 Mapper.C
从存储过程返回的数据有 3 列重复数据: Name | Address | PhoneNumber | UniqueCol1 | UniqueCol2 理想情况下,我希望我的模型通过仅存储一次值并收集
当我使用 List 属性映射对象时,默认情况下 Automapper 会将目标对象的 list 属性设置为源对象的实例。 有没有办法让自动映射器创建一个新列表并复制项目但不复制列表实例? 我希望通过以
如果我有这组源类怎么办: namespace Source { class CA { public CB B { get; set; } } class
我是一名优秀的程序员,十分优秀!