Entity Framework 代码先行

**二、Code
First
的演示**

public class Resource
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Controller { get; set; }
    public string Action { get; set; }
    public string IconCls { get; set; }
    public Nullable<int> ParentId { get; set; }
    public int Sort { get; set; }
    public int Category { get; set; }
    [ForeignKey("ParentId")]
    public virtual Resource ParentResource { get; set; }
    public virtual ICollection<Resource> ChildResources { get; set; }
    public virtual ICollection<RoleResource> RoleResources { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Remark { get; set; }
    public virtual ICollection<RoleResource> RoleResources { get; set; }
    public virtual ICollection<UserRole> UserRoles { get; set; }
}

public class RoleResource
{
    public int Id { get; set; }
    public int RoleId { get; set; }
    public int ResourceId { get; set; }

    [ForeignKey("RoleId")]
    public virtual Role Role { get; set; }
    [ForeignKey("ResourceId")]
    public virtual Resource Resource { get; set; }
}

public class User
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string RealName { get; set; }
    public bool Gender { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Picture { get; set; }
    public bool IsValid { get; set; }
    public string Remark { get; set; }
    public string Theme { get; set; }
}

public class UserRole
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int RoleId { get; set; }

    [ForeignKey("RoleId")]
    public virtual Role Role { get; set; }
    [ForeignKey("UserId")]
    public virtual User User { get; set; }
}

public class EasyUIContext : DbContext
{
    public EasyUIContext()
        : base("name=EasyUIConnectString")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //防止数据库生成的表是复数形式
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }

    public DbSet<Resource> Resource { get; set; }
    public DbSet<Role> Role { get; set; }
    public DbSet<RoleResource> RoleResource { get; set; }
    public DbSet<User> User { get; set; }
    public DbSet<UserRole> UserRole { get; set; }
    public DbSet<Log> Log { get; set; }
}

新建Resource、Role、RoleResource、User、UserRole、以及 EasyUIContext类

Code First是基于Entity
Framework的新的开销方式,原先唯有Database First和Model First三种。Code
First顾名思义,便是先用C#/VB.NET的类定义好您的圈子模型,然后用那几个类映射到存活的数据库只怕发生新的数据库结构。Code
First同样帮衬通过Data Annotations或fluent
API进行定制化配置。

3、数据库连接字符串

图片 1

图片 2

 

二、添加引用

为了扶助以统一筹划为主导的费用流程,EF还更加多地支撑以代码为大旨(code-centric)
,大家称为代码优先的支出,代码优先的支出援助特别雅观的付出流程,它同意你在不选取设计器恐怕定义七个XML 映射文件的情状下开始展览付出。

一、新建类库 EasyUI.Entities

  • 允许编写简单的模子对象POCO
    (plain old classes),而不要求基类。
  • 经过”约定优于配备”,使得数据库持久层不需求任何的安排
  • 也足以覆盖”约定优于配备”,通过流畅的 API
    来完全定制持层的照耀。

3、新建类

一、什么是Code
First

  <connectionStrings>
    <add name="EasyUIConnectString" connectionString="server=LXHPGCA3R0P9HFU;uid=sa;pwd=******;database=EasyUI" providerName="System.Data.SqlClient" />
  </connectionStrings>