实体定义

实体定义

实体为纯 POCO,支持可空类型以及枚举类型映射。

public enum Gender
{
    Male = 1,
    Female = 2
}
[TableAttribute("Person")]
public class Person
{
    [Column(IsPrimaryKey = true)]
    public int Id { get; set; }
    [Column(DbType = DbType.String)]
    public string Name { get; set; }
    [Column(DbType = DbType.Int32)]
    public Gender? Gender { get; set; }
    public int? Age { get; set; }
    public int? CityId { get; set; }

    /// <summary>
    /// 更新实体时不更新此字段
    /// </summary>
    [UpdateIgnoreAttribute]
    public DateTime CreateTime { get; set; } = DateTime.Now;
    public DateTime? EditTime { get; set; }

    [NotMapped]
    public string NotMapped { get; set; } //不映射任何字段

    //[Column(IsRowVersion = true)]
    //public int RowVersion { get; set; }

    [Navigation("Id")]
    public PersonEx Ex { get; set; } /* 1:1 */
    [NavigationAttribute("CityId")]
    public City City { get; set; }
}
public class PersonEx
{
    [Navigation("Id")]
    public Person Owner { get; set; }  /* 1:1 */

    [Column(IsPrimaryKey = true)]
    [NonAutoIncrement]
    public int Id { get; set; }
    public string IdNumber { get; set; }
    public DateTime? BirthDay { get; set; }
}
public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ProvinceId { get; set; }

    [NavigationAttribute("ProvinceId")]
    public Province Province { get; set; }

    [NavigationAttribute]
    public List<Person> Persons { get; set; } = new List<Person>();
}
public class Province
{
    public int Id { get; set; }
    public string Name { get; set; }

    [NavigationAttribute]
    public List<City> Cities { get; set; } = new List<City>();
}