I’m trying to use table splitting with EF Core 8 and I’m having some issues with the mappings. Here’s what my types look like:
<code>public class Employee
{
public int Id{ get; set; }
public int Version{get; set}
public string Name{ get; set; }
// other properties
}
public class EmployeeSummary
{
public int Id{ get; set; }
public string Name{ get; set; }
public Employee Emnployee { get;set;}
}
</code>
<code>public class Employee
{
public int Id{ get; set; }
public int Version{get; set}
public string Name{ get; set; }
// other properties
}
public class EmployeeSummary
{
public int Id{ get; set; }
public string Name{ get; set; }
public Employee Emnployee { get;set;}
}
</code>
public class Employee
{
public int Id{ get; set; }
public int Version{get; set}
public string Name{ get; set; }
// other properties
}
public class EmployeeSummary
{
public int Id{ get; set; }
public string Name{ get; set; }
public Employee Emnployee { get;set;}
}
And here are the mappings:
<code>public sealed class EmployeeTypeConfiguration: IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.ToTable("Employees");
builder.HasKey(f => f.Id);
builder.Property(f => f.Id).HasColumnName("EmpId");
builder.Property(f => f.Version).IsConcurrencyToken( );
builder.Property(f => f.Name).HasColumnName("Name");
// other mappings
}
}
public sealed class EmployeeSummaryTypeConfiguration: IEntityTypeConfiguration<EmployeeSummary>
{
public void Configure(EntityTypeBuilder<EmployeeSummary> builder)
{
builder.ToTable("Employees");
builder.HasKey(f => f.Id);
builder.Property(f => f.Id).HasColumnName("EmpId");
builder.Property<int>("Version").IsConcurrencyToken( );
builder.Property(e => e.Name).HasColumnName("Name");
builder.HasOne(f => f.Employee)
.WithOne( )
.HasForeignKey<Employee>(f1 => f1.Id);
builder.Navigation(f => f.Employee).IsRequired( );
}
}
</code>
<code>public sealed class EmployeeTypeConfiguration: IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.ToTable("Employees");
builder.HasKey(f => f.Id);
builder.Property(f => f.Id).HasColumnName("EmpId");
builder.Property(f => f.Version).IsConcurrencyToken( );
builder.Property(f => f.Name).HasColumnName("Name");
// other mappings
}
}
public sealed class EmployeeSummaryTypeConfiguration: IEntityTypeConfiguration<EmployeeSummary>
{
public void Configure(EntityTypeBuilder<EmployeeSummary> builder)
{
builder.ToTable("Employees");
builder.HasKey(f => f.Id);
builder.Property(f => f.Id).HasColumnName("EmpId");
builder.Property<int>("Version").IsConcurrencyToken( );
builder.Property(e => e.Name).HasColumnName("Name");
builder.HasOne(f => f.Employee)
.WithOne( )
.HasForeignKey<Employee>(f1 => f1.Id);
builder.Navigation(f => f.Employee).IsRequired( );
}
}
</code>
public sealed class EmployeeTypeConfiguration: IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.ToTable("Employees");
builder.HasKey(f => f.Id);
builder.Property(f => f.Id).HasColumnName("EmpId");
builder.Property(f => f.Version).IsConcurrencyToken( );
builder.Property(f => f.Name).HasColumnName("Name");
// other mappings
}
}
public sealed class EmployeeSummaryTypeConfiguration: IEntityTypeConfiguration<EmployeeSummary>
{
public void Configure(EntityTypeBuilder<EmployeeSummary> builder)
{
builder.ToTable("Employees");
builder.HasKey(f => f.Id);
builder.Property(f => f.Id).HasColumnName("EmpId");
builder.Property<int>("Version").IsConcurrencyToken( );
builder.Property(e => e.Name).HasColumnName("Name");
builder.HasOne(f => f.Employee)
.WithOne( )
.HasForeignKey<Employee>(f1 => f1.Id);
builder.Navigation(f => f.Employee).IsRequired( );
}
}
After adding EmployeeSummary
, I’m no longer able to insert a new Employee:
<code>Employee emp = new("123123123");
employee.UpdateWorkPlace(newWorkplace);
// more code
dbContext.Set<Employee>().Add(emp)
await dbContext.SaveChangesAsync(); // throws exception
</code>
<code>Employee emp = new("123123123");
employee.UpdateWorkPlace(newWorkplace);
// more code
dbContext.Set<Employee>().Add(emp)
await dbContext.SaveChangesAsync(); // throws exception
</code>
Employee emp = new("123123123");
employee.UpdateWorkPlace(newWorkplace);
// more code
dbContext.Set<Employee>().Add(emp)
await dbContext.SaveChangesAsync(); // throws exception
When I run the previous snippet, I’ll end up with the following error:
System.InvalidOperationException
The value of ‘Employee.Id’ is unknown when attempting to save changes. This is because the property is also part of a foreign key for which the principal entity in the relationship is not known.
What am I doing wrong?
Thanks.