[entity framework core] Entity Framework Core Type Configuration
https://www.learnentityframeworkcore.com/configuration/fluent-api/type-configuration
how to use?
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(typeof(EntityName))...
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity("EntityName")...
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityName>()...
}
method | des |
---|---|
HasAlternateKey | Generates a unique constraint for the specified property or properties |
HasAnnotation | Provides a means to apply annotations via the Fluent API |
HasBaseType | Specifies the base type of the entity |
HasIndex | Generates an index on the specified property or properties |
HasKey | Denotes the specified property as the entity key |
HasMany | Specifies the Many end of a relationship |
HasOne | Specifies the One end of a relationship |
Ignore | Denotes that the entity should be omitted from mapping |
ToTable | Specifies the database table that the entity should be mapped to |
Property | Provides access to property configuration |
example
public class SampleContext : DbContext
{
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.HasKey(o => o.OrderNumber);
}
}
public class Order
{
public int OrderNumber { get; set; }
public DateTime DateCreated { get; set; }
public Customer Customer { get; set; }
...
)
public class SampleContext : DbContext
{
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.HasKey(o => new { o.CustomerAbbreviation, o.OrderNumber });
}
}