当前位置 : 主页 > 网页制作 > HTTP/TCP >

[entity framework core] Entity Framework Core Type Configuration

来源:互联网 收集:自由互联 发布时间:2021-06-16
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 OnMod

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 });
    }
}
网友评论