我有以下图表:
public class Report
{
public Guid Id { get; set; }
public ICollection<EmployeeEntry> EmployeeEntries { get; set; }
}
public class EmployeeEntry
{
public Guid Id { get; set; }
public DateTimeOffset EntryDate { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string City { get; set; }
public string State { get; set; }
}
EmployeeEntry
private void ConfigureEmployeeEntry(EntityTypeBuilder<EmployeeEntry> builder)
{
builder.OwnsOne(x => x.Address, w =>
{
w.Property(x => x.City).HasMaxLength(100);
w.Property(x => x.State).HasMaxLength(100);
});
}
但是当我有一个以上相同的地址
dbContext.Reports.Add(report);
dbContext.SaveChanges();
我得到了以下例外:
"报告。EmployeeEntries'具有类型为的实体
“雇员入境。地址#Address',但不存在此类型的实体
具有已标记为“已添加”的相同键值。考虑
使用“DbContextOptionsBuilder”。启用SensitiveDatalogging'以查看
关键值。
SaveChanges()
EF将地址设为
null
.
我使用的是EntityFramework核心的最新版本。