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>
Entity Framework 代码先行的更多相关文章
- Entity Framework 代码先行之约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
- Entity Framework 数据库先行、模型先行、代码先行
数据库先行(Database First):基于已存在的数据库,利用某些工具(如Vs提供的EF设计器)创建实体类,数据库对象与实体类的匹配关系等,你也可以手动修改这些自动生成的代码及匹配文件. 模型先 ...
- Entity Framework:代码优先
一.代码优先Code First EF6支持Oracle ODT 12C Release 3 (net4.5) DataModel(类)-->生成数据库DB 或 存在的数据库DB-->生成 ...
- Entity Framework 6.X实现记录执行的SQL功能
Entity Framework在使用时,很多时间操纵的是Model,并没有写sql语句,有时候为了调试或优化等,又需要追踪Entity framework自动生成的sql(最好还能记录起来,方便出错 ...
- 使用 Entity Framework Core 时,通过代码自动 Migration
一 介绍 在使用 Entity Framework Core (下面就叫 EF Core 吧)进行开发时,如果模型有变动,我们要在用 EF Core 提供的命令行工具进行手工迁移,然后再运行程序.但是 ...
- Visual Studio Entity Framework (EF) 生成SQL 代码 性能查询
Visual Studio Entity Framework (EF) 生成SQL 代码 性能查询 SQL 中,有SQL Server Profiler可以用来查询性能以及查看外部调用的SQL ...
- Entity Framework 5.0系列之自动生成Code First代码
在前面的文章中我们提到Entity Framework的"Code First"模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Framework P ...
- Entity Framework 实体框架的形成之旅--几种数据库操作的代码介绍(9)
本篇主要对常规数据操作的处理和实体框架的处理代码进行对比,以便更容易学习理解实体框架里面,对各种数据库处理技巧,本篇介绍几种数据库操作的代码,包括写入中间表操作.联合中间表获取对象集合.递归操作.设置 ...
- Entity Framework 自动生成CodeFirst代码
前言 在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Framework Power To ...
随机推荐
- Linux 服务器监控
200 ? "200px" : this.width)!important;} --> 标签:iostat/free/top/dstat 概述 文字主要讲述使用linux自带 ...
- Distributed MVCC based cross-row transaction
The algorithm for supporting distributed MVCC based cross-row transactions on top of a distributed k ...
- 学习Cassandra的开源电子书(中英文版)
学习Cassandra的开源电子书(中英文版)发布啦:http://teddymaef.github.io/learncassandra/ 之前发布了英文版,现在包含中文版了. 学习Cassandra ...
- TODO:Node.js pm2使用方法
TODO:Node.js pm2使用方法 pm2 是一个带有负载均衡功能的Node应用的进程管理器. 当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完 ...
- Spark读取HBase
背景:公司有些业务需求是存储在HBase上的,总是有业务人员找我要各种数据,所以想直接用Spark( shell) 加载到RDD进行计算 摘要: 1.相关环境 2.代码例子 内容 1.相关环境 Spa ...
- PHP fsockopen 异步调用接口在nginx上偶尔实效的情况
private function fsock_asy_do($get){ $fp = fsockopen("ssl://www.xxx.com", 443, $errno, $er ...
- Android开发-之第一个程序:HelloWorld!
小编觉得不管学习什么编程的时候,第一个程序都是要求打印输出一个"HelloWorld!",那就从最简单的HelloWorld开始吧!哈哈~~~~ 一.创建一个Android工程 1 ...
- SSISDB2:使用TSQL执行Package
在SSISDB中,能够使用TSQL脚本执行Package:每执行一次Package,SSIS都会创建一个Operation 和一个执行实例(Execution Instance),每个Executio ...
- SQL Server 创建数据库邮件
一. 背景 数据库发邮件通知数据库的运行状态(状态可以通过JOB形式获取)和信息,达到预警的效果. 二. 基础知识 msdb系统数据库保存有关Job,Database Mail,Nodifyicati ...
- 处理UnicodeDecodeError: ‘XXX' codec can't decode bytes in position...的问题
错误信息: UnicodeDecodeError: ‘XXX' codec can't decode bytes in position 2-5: illegal multibyte sequence ...