首页 > 代码库 > EF 关系描述

EF 关系描述

网络上常常看到有ef 1对1 1对多等关系的描述,按照我的理解,其根本就是为了呈现出一个视图,我最近设计了一个ef关系,请大家看一看。

 

需求描述

在gps车辆信息管理中,有个开户需求,其根本就是三种关系的对应,车辆信息,sim卡信息,以及终端信息,这三个实体是一一对应的,一旦开户,就需要三种信息。

先设计三种关系实体

    /// <summary>    /// 实体模型抽象类    /// </summary>    public abstract class DbSetBase : IDbSetBase    {        /// <summary>        /// 构造函数        /// </summary>        public DbSetBase()        {            Id = Guid.NewGuid();            CreatedDate = DateTime.Now;            Deleted = false;        }        /// <summary>        /// 唯一Id        /// </summary>        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]        public Guid Id { get; set; }        /// <summary>        /// 创建时间        /// </summary>        public DateTime CreatedDate { get; set; }        /// <summary>        /// 更新时间        /// </summary>        public DateTime? UpdatedDate { get; set; }        /// <summary>        /// 是否删除        /// </summary>        public bool Deleted { get; set; }        /// <summary>        /// 备注        /// </summary>        public string Remark { get; set; }    }

 

    /// <summary>    /// 车辆数据    /// </summary>    [Table("S_VehicleInfo")]    public class VehicleInfo : DbSetBase    {        /// <summary>        /// 车牌号        /// </summary>        public string VehicleNo { get; set; }        /// <summary>        /// 车牌颜色        /// </summary>        public VehicleColor Color { get; set; }        /// <summary>        /// 企业信息        /// </summary>        public Guid EnterpriseId { get; set; }        /// <summary>        /// 企业信息        /// </summary>        [ForeignKey("EnterpriseId")]        public virtual Enterprise Enterprise { get; set; }        /// <summary>        /// 使用状态        /// </summary>        public UsingType UsingType { get; set; }        /// <summary>        /// 开户信息        /// </summary>        public virtual OpenAccount Account { get; set; }        /// <summary>        /// 所属车队        /// </summary>        public Guid? CompanyId { get; set; }        /// <summary>        /// 所属车队        /// </summary>        [ForeignKey("CompanyId")]        public virtual Company Company { get; set; }    }

 

    /// <summary>    /// 终端信息    /// </summary>    [Table("S_TerminalInfo")]    public class TerminalInfo : DbSetBase    {        /// <summary>        /// 终端Id        /// </summary>        public string TerminalNo { get; set; }        /// <summary>        /// 终端类型        /// </summary>        public string TerminalType { get; set; }        /// <summary>        /// 使用状态        /// </summary>        public UsingType UsingType { get; set; }        /// <summary>        /// 开户信息        /// </summary>        public virtual OpenAccount Account { get; set; }    }

 

    /// <summary>    /// Sim信息    /// </summary>    [Table("S_SimInfo")]    public class SimInfo : DbSetBase    {        /// <summary>        /// 手机号        /// </summary>        public string PhoneNo { get; set; }        /// <summary>        /// Sim卡号        /// </summary>        public string SimNo { get; set; }        /// <summary>        /// 使用状态        /// </summary>        public UsingType UsingType { get; set; }        /// <summary>        /// 开户信息        /// </summary>       public virtual OpenAccount Account { get; set; }    }

 

    /// <summary>    /// 开户信息    /// </summary>    [Table("OpenAccount")]    public class OpenAccount : DbSetBase    {        /// <summary>        ///  终端信息        /// </summary>        public virtual TerminalInfo Terminal { get; set; }        /// <summary>        /// 车辆信息        /// </summary>        public virtual VehicleInfo Vehicle { get; set; }        /// <summary>        /// Sim信息        /// </summary>        public virtual SimInfo Sim { get; set; }        /// <summary>        /// 注册时间        /// </summary>        public DateTime? RegisterTime { get; set; }        /// <summary>        /// 是否注册        /// </summary>        public bool? IsRegister { get; set; }    }}

 ,在通过 Fluent Api设计其对应关系:

 modelBuilder.Entity<OpenAccount>().HasRequired(a => a.Vehicle).WithOptional(b => b.Account).Map(m => m.MapKey("VehicleId")); modelBuilder.Entity<OpenAccount>().HasRequired(a => a.Sim).WithOptional(b => b.Account).Map(m => m.MapKey("SimId")); modelBuilder.Entity<OpenAccount>().HasRequired(a => a.Terminal).WithOptional(b => b.Account).Map(m => m.MapKey("TerminalId"));

 

 

 

这样三种对象的对应关系就完成了,开户信息表就是作为一种视图来呈现三种对象的对应。

 

EF 关系描述