I have the following graph:
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; }
}
And using fluentApi I have configured Address as owned entity by EmployeeEntry
entity like the following:
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);
});
}
But when I have the same Address for more than one EmployeeEntry
and running the following code:
dbContext.Reports.Add(report);
dbContext.SaveChanges();
I got the following exception:
The entity of type 'EmployeeEntry' is sharing the table 'report.EmployeeEntries' with entities of type 'EmployeeEntry.Address#Address', but there is no entity of this type with the same key value that has been marked as 'Added'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.
As I noticed that after adding the report before SaveChanges()
EF makes Address as null
.
I am using the latest version of the EntityFramework core.