在映射值对象到数据库表的时候配置错误会报错误,
错误为:The type "xx" cannot be marked as owned because a non-owned entity type with the same name already exists.
仔细检查之后记录下正确的配置
如下例子中User为实体,包含Salary值对象和AddressList值对象列表两个属性。目前的实现是将Salary的属性Number和Unit平铺到User表中,作为两列。Address映射到新的表。
[Table(User)]
public class User : Entity<long>
{
public Salary Salary {get; set;}
public List<Address> AddressList {get; set;}
}
[Owned]
public class Salary : ValueObject
{
public double Number {get; set;}
public string Unit {get; set;}
}
[Owned]
public class Address : ValueObject
{
public string Province {get; set;}
public string Street {get; set;}
}
正确的配置为:
1,值对象继承 Abp.Domain.Values.ValueObject这个类
2,(1)用 [Owned] 属性标签标记
(2)在配置文件EntityTypeConfiguration中添加配置
builder.OwnsOne(e => e.Salary);
builder.OwnsMany(e => e.AddressList)
.ToTable("UserAddress")
.WithOwner()
.HasForeignKey(p => p.UserId);
本文介绍如何正确配置实体和值对象的映射关系,避免错误如类型'xx'不能被标记为拥有,因为已存在同名的非拥有实体类型。通过具体的代码示例展示如何使用[Owned]属性及EntityTypeConfiguration配置。

5936

被折叠的 条评论
为什么被折叠?



