Updating entity Persons
I get the following error:
System.InvalidOperationException: The property 'ID' on entity type 'Person' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.
at Microsoft.EntityFrameworkCore....
This has been discussed several places, and a similar issue was reported fixed by the EF Core team. However, one of those posts is about multiple updates to an entity and the one-to-many solution does not work here; additionally, I cannot make one ID column nullable and prefer to use the fluent API configuration. The documentation example does not work either, so I am asking here.
The scenario is that I am upgrading a legacy ASP.NET MVC 4 project to ASP.NET MVC Core, and as a result I am upgrading from EF 6.1 to EF Core 2.1. I will happily move to 2.2 instead if it solves this problem; I think it was still in prerelease when I started.
Here is a (ridiculously) simplified version of my entities:
public class Person
{
public int ID { get; set; }
public string name { get; set; }
public virtual Worker Worker { get; set; }
}
public class Worker
{
public int ID { get; set; }
public string somePersonalDetails { get; set; }
public virtual Person Person { get; set; }
// other relationships exist
}
I am using fluent API configuration:
public class PersonBuilder
{
public PersonBuilder(EntityTypeBuilder<Person> entity)
{
entity.HasKey(k => k.ID);
entity.HasOne(p => p.Worker)
.WithOne(p => p.Person)
.HasForeignKey<Person>(p => p.ID)
//.IsRequired(false) //?
.OnDelete(DeleteBehavior.Cascade);
}
}
public class WorkerBuilder
{
public WorkerBuilder(EntityTypeBuilder<Worker> entity)
{
entity.HasKey(k => k.ID);
// other relationships are defined
}
}
public override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Configurations<Person>().Add(typeof(PersonBuilder));
builder.Configurations<Worker>().Add(typeof(WorkerBuilder));
}
The reason that it's split apart like that is because I adapted it from our leftover EF 4/5/6 configuration. Yay legacy code. Nevertheless it works (for other defined types). The way I am reading that, it says "define a foreign key on the related Worker
object pointing to the ID of this object." It does just the opposite.
I have tried:
Defining the key relationship on the WorkerBuilder type instead. This yields
SQLite Error 19: 'FOREIGN KEY constraint failed'.
Amazingly, however, it still attempts to define the key on the Person entity, which is wrong.Removing some of the specific expressions in hopes that EF will just figure out the relationship itself. It doesn't; if I provide too little information, it tries to use columns|fields that don't exist (e.g., "PersonID", or is unable to figure out the relationship altogether.
So, I am stumped. Has anyone done this successfully? In plain English,
"A person may or may not have a worker record" (1:0); and, "if they have a worker record, both records have the same ID." (FK_W_ID__P_ID)