I have this situation:
The second table is not inheriting the first one. It will contain different information
I'm using EF Core and I've trouble configuring the models for the two entities. I work with fluent API configurations and using EF Core Migrations to create the tables. The models and their configurations are as follows:
public class Table1
{
public long Id { get; set; }
public string FirstName { get; set; }
public List<Table2> T2 { get; set; }
}
public class Table1Configuration : IEntityTypeConfiguration<Table1>
{
public void Configure(EntityTypeBuilder<Table1> builder)
{
builder.ToTable("table1");
builder.Property(s => s.Id)
.HasColumnName("id")
.HasColumnType("serial");
builder.Property(s => s.FirstName)
.HasColumnName("first_name");
}
}
The second one:
public class Table2
{
public long Table1Id { get; set; }
public DateTime Timestamp { get; set; }
public double Value { get; set; }
public Table1 T1 { get; set; }
}
public class Table2Configuration : IEntityTypeConfiguration<Table2>
{
public void Configure(EntityTypeBuilder<Table2> builder)
{
builder.ToTable("table2");
builder.Property(s => s.Table1Id)
.HasColumnName("table1_id");
builder.HasNoKey();
builder.Property(s => s.Timestamp)
.HasColumnName("ts");
builder.Property(s => s.Value)
.HasColumnName("value");
builder.HasOne(s => s.T1)
.WithMany(s => s.T2)
.HasForeignKey(s => s.Table1Id);
}
}
When I run the Update-Database
command it throws exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
System.InvalidOperationException: The navigation '' cannot be added because it targets the keyless entity type 'Table2'. Navigations can only target entity types with keys.
How can we have a table with only foreign keys? I do not want to put an extra column for a primary key, because the second table will be flushed frequently
If I miss some information that will be helpful, please ask me to include in in the comments!
Thanks in advance, Julian
As anybody stumbles the same problem, as @Bryan Lewis suggested in the comment, I've just added a primary key to the table
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.