First CTP4 code: table without primary key?

With Entity Framework and Code First, is it possible to create and use a table without primary keys? I can not get this installation to work:

public class Report
{
    public virtual int ReportId
    public virtual ICollection<ReportChanges> ReportChanges
}

public class ReportChanges
{
    public virtual Report Report
    public virtual string EditorName
    public virtual DateTime Changed
}

Note that I excluded the primary key assignment in ReportChanges. But with this setting, I get: "It is not possible to derive a key for the" ReportChanges "object type.

Either I'm missing something, or Code First does not support tables without primary keys. What is right? Thank.

+3
source share
2 answers

EF PI, , PK. , EF , PK, , Report Changed.

+1

EF . , , Google Google:

public class YourDataContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ReportChanges>().HasKey(x => new {x.Report, x.Changed});
    }
}

"new {x.Report, x.Changed}" , EF .

+1

Source: https://habr.com/ru/post/1772459/


All Articles