I'm using Entity Framework Core 5.0 for SQLite and have such schema:
CREATE TABLE Elem (
id INTEGER NOT NULL PRIMARY KEY,
title TEXT NOT NULL
);
CREATE TABLE Property (
elemId INTEGER NOT NULL,
name TEXT NOT NULL,
value FLOAT NOT NULL
);
I created the following classes:
[Table("Elem")]
public class dbElem {
[Key]
public long id { get; set; }
public string name { get; set; }
public virtual ICollection<Property> props { get; set; }
}
[Table("Property")]
public class dbProperty {
public long elemId { get; set; }
public virtual dbElem elem { get; set; }
public string name { get; set; }
public double value { get; set; }
}
In the OnModelCreating function, I wrote the following:
modelBuilder.Entity<dbProperty>(entity => {
entity.HasNoKey();
entity.HasOne(f => f.elem).WithMany(f => f.props).HasForeignKey(f => f.elemId).OnDelete(DeleteBehavior.Cascade);
});
Everything looks logical, but I get an error: System.InvalidOperationException: 'The navigation '' cannot be added because it targets the keyless entity type 'dbProperty'. Navigations can only target entity types with keys.'
Is there any reason for this restriction?
Can I somehow influence the EFC to not add a key?