在映射值对象到数据库表的时候配置错误会报错误,
错误为: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);