Fluent Mapping

Fluent Mapping

1. 创建实体

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Gender? Gender { get; set; }
    public int? Age { get; set; }
    public int? CityId { get; set; }
    public DateTime? OpTime { get; set; }
    public string NotMapped { get; set; }
}

2. 创建映射关系
建一个类继承于 Chloe.Entity.EntityTypeBuilder<TEntity>。

public class UserMap : EntityTypeBuilder<User>
{
    public UserMap()
    {
        this.MapTo("Users");
        this.Property(a => a.Id).IsAutoIncrement().IsPrimaryKey();
        this.Ignore(a => a.NotMapped);
        this.Property(a => a.Gender).HasDbType(DbType.Int32);
    }
}

3. 注册 UserMap
在应用程序启动时注册 UserMap。

DbConfiguration.UseTypeBuilders(typeof(UserMap));