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

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>
原文地址:https://www.cnblogs.com/weiweixiang/p/5504276.html