EF HasNoKey
public class ExampleDbContext:DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<LOG>().HasNoKey().ToTable("LOG"); //No primay key
}
}
void OnModelCreating(ModelBuilder modelBuilder)
public class ExampleDbContext:DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<LOG>().HasNoKey().ToTable("LOG"); //No primay key
}
}
• EF Core calls this method in the class that derives from the DbContext class.
• Here you can make table-class mappings, relationships, column settings, etc.
protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder)
• It also calls the OnModelCreating of the superclass (DbContext).
• So you don't disturb EF's own default settings.
modelBuilder.Entity<LOG>()
• You are preparing to connect the entity class named LOG to a table in the database.
• So even though you have defined DbSet
.HasNoKey()
• Normally, EF Core expects a Primary Key (Id field) for each entity.
• You're saying, "This entity doesn't have a key field, so don't track changes; use it only for queries."