Entity Framework 代码先行

一、什么是Code
First

为支持因规划呢骨干的开销流程,EF还再次多地支持以代码为着力
(code-centric)
,我们叫代码优先的支付,代码优先的支出支持逾美妙之付出流程,它同意你当非应用设计器或者定义一个
XML 映射文件的景象下进行支付。

  • 兴编写简单的型对象POCO
    (plain old classes),而不待基类。
  • 经过”约定优于配备”,使得数据库持久层不需另的配置
  • 也得挂”约定优于配备”,通过流畅的 API
    来了定制持层的炫耀。

Code First是基于Entity
Framework的初的开模式,原先只有Database First和Model First两栽。Code
First顾名思义,就是先用C#/VB.NET的类定义好而的领域模型,然后用这些类映射到现有的数据库或者发生新的数据库结构。Code
First同样支撑通过Data Annotations或fluent
API进行定制化配置。

**二、Code
First
的演示**

1、新建类库 EasyUI.Entities

图片 1

2、添加引用

图片 2

3、新建类

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

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; }
}

3、数据库连接字符串

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