模型构建


  如果你觉得实体类不够简洁,你可以把实体映射去掉(详见 实体映射)的相关特性都移走,而在 EntityContext 的子类里重写 OnModelCreating 方法来实现映射模型的构建,这和 Entity Framework 非常相似。如下所示:

public class DbContext : EntityContext
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Orders>(e =>
            {
                e.Property(p => p.OrderID)
                    .IsPrimaryKey()
                    .HasIdentity(IdentityGenerateType.AutoIncrement);

                e.Property(p => p.CustomerID)
                    .HasColumnName("customerid");
                e.Property(p => p.EmployeeID)
                    .HasColumnName("employeeid)";
                e.Property(p => p.OrderDate)
                    .HasColumnName("orderdate)";

                e.HasMany(r => r.OrderDetailses)
                    .WithOne()
                    .HasPrimaryKey(t => t.OrderID)
                    .HasForeignKey(t => t.OrderID);

                e.HasOne(r => r.Customers)
                    .WithMany()
                    .HasPrimaryKey(t => t.CompanyName)
                    .HasForeignKey(t => t.CustomerID);
            })
            .ToTable("orders");

        builder.Entity<OrderDetails>(e =>
            {
                e.Property(p => p.OrderID).IsPrimaryKey();
                e.Property(p => p.Product1ID).IsPrimaryKey();
            })
            .ToTable("order details");

        builder.Entity<Customers>(e =>
            {
                e.Property(p => p.CustomerID).IsPrimaryKey();
            })
            .ToTable("customers");

        builder.Entity<Products>(e =>
            {
                e.Property(p => p.Id)
                    .HasColumnName("product_id").IsPrimaryKey();
                e.Property(p => p.ProductName)
                    .HasColumnName("product_name")
                    .HasValidation(v => v.Required().StringLength(50)); //数据验证

                e.HasMany(r => r.OrderDetailses)
                    .WithOne()
                    .HasPrimaryKey(t => t.Id)
                    .HasForeignKey(t => t.ProductId);
            })
            .ToTable("products");

        base.OnModelCreating(builder);
    }
}

  修改后的实体类如下所示(示例):

public partial class Products : LightEntity<Products>
{
    public virtual int Id { get; set; }

    public virtual string ProductName { get; set; }

    public virtual int? SupplierID { get; set; }

    public virtual int? CategoryID { get; set; }

    public virtual string QuantityPerUnit { get; set; }

    public virtual decimal? UnitPrice { get; set; }

    public virtual short? UnitsInStock { get; set; }

    public virtual short? UnitsOnOrder { get; set; }

    public virtual short? ReorderLevel { get; set; }

    public virtual bool? Discontinued { get; set; }

    public virtual EntitySet<OrderDetails> OrderDetailses { get; set; }
}

💡 小提示

  除了主键和关联属性,其他属性如果字段名称一致的话,可以不用映射。关联属性必须映射,否则关联查询的 lambda 表达式将无法解析。

  使用模型构建的元数据,EntityMetadataUnityPropertyUnityRelationshipUnity 类的方法依然能够获取到相关的元数据,因为它们使用的是同一套元数据机制。