DAL層的三個Model類: 字典表:CFDict 用戶表:CFUser 用戶愛好表:CFUserHobby(關聯cfuser表和cfdict表) CFUser表和CFUserHobby表是一對多關係,一個用戶有多個愛好,CFDict表和CFUserHobby表是一對多關係,某一個愛好可能很多人都有 ...
DAL層的三個Model類:
字典表:CFDict
用戶表:CFUser
用戶愛好表:CFUserHobby(關聯cfuser表和cfdict表)
CFUser表和CFUserHobby表是一對多關係,一個用戶有多個愛好,CFDict表和CFUserHobby表是一對多關係,某一個愛好可能很多人都有
public class CFDict
{
//標識列
public int Id { get; set; }
//名稱
public string Name { get; set; }
//父ID
public int ParentId { get; set; }
//所在層,從0開始
public int Level { get; set; }
//所屬分類
public string Category { get; set; }
//排序號
public int? OrderNum { get; set; }
public virtual ICollection<CFUserHobby> CFUserHobbys { get; set; }
}
public class CFUser
{
public int Id { get; set; }
//用戶名
[StringLength(50)]
public string UserName { get; set; }
//密碼
[StringLength(50)]
public string UserPwd { get; set; }
//郵箱
[StringLength(50)]
public string UserMail { get; set; }
//全名
[StringLength(50)]
public string FullName { get; set; }
//性別
public int? Sex { get; set; }
//出生日期
public DateTime? BirthDay { get; set; }
//城市code
public int? CityCode { get; set; }
//愛好code
public int? HobbyCode { get; set; }
//博客網站
public int? BlogCode { get; set; }
//博客名稱
public string BlogName { get; set; }
//註冊日期
public DateTime SignDate { get; set; }
public virtual ICollection<CFUserHobby> CFUserHobbys { get; set; }
}
public class CFUserHobby
{
//標識列
public int Id { get; set; }
//用戶ID
public int CFUserId { get; set; }
//愛好ID
public int CFDictId { get; set; }
public virtual CFUser CFUser { get; set; }
public virtual CFDict CFDict { get; set; }
}
public class CFContext : DbContext
{
public CFContext()
: base("CFContext")
{
}
public DbSet<CFUser> CFUsers { get; set; }
public DbSet<CFDict> CFDicts { get; set; }
public DbSet<CFUserHobby> CFUserHobbys { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
可以看到生成的外鍵:
源代碼下載QQ群:389496325