EF有三种开发模式:Model First,Database First 和 Code First。 本文主要介绍Code First模式,Code First即代码优先,就是先编写业务实体模型类,然后通过程序包管理器控制台创建数据库。
在项目上右键菜单,点击“管理NuGet程序包”,搜索“EntityFramework”,在项目中安装最新版本的EntityFramework。
EntityFramework安装完成后,会自动生成一个App.confing文件,内容如下:
class="code_img_closed" src="/Upload/Images/2013100202/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('a8629840-5a6f-423b-8b26-b84b437ac9a6',event)" src="/Upload/Images/2013100202/2B1B950FA3DF188F.gif" alt="" /><?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> </entityFramework> </configuration>View Code
如果不添加数据库连接字符串,数据库默认会声称在App_Data文件夹下,在一般项目中我们还需修改此配置文件,内容如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <!--<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />--> </configSections> <!--<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> <parameter value="shz-pc\sqlexpress2012"/> </parameters> </defaultConnectionFactory> </entityFramework>--> <connectionStrings> <add name="SAUnitOfWork" connectionString="Data Source=shz-pc\SQLEXPRESS2012;Initial Catalog=AccountingSys;User ID=sa;Password=sa" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>View Code
贴出部分实例代码:
/// <summary> /// 用户信息表 /// </summary> public class User : Entity { [Required] [StringLength(50)] /// <summary> /// 用户名 /// </summary> public string Name { get; set; } [Required] [StringLength(20)] /// <summary> /// 登录Id /// </summary> public string LoginId { get; set; } [Required] [StringLength(20)] /// <summary> /// 登录密码 /// </summary> public string Password { get; set; } [StringLength(11)] /// <summary> /// 手机号 /// </summary> public string Mobile { get; set; } [StringLength(100)] /// <summary> /// 邮箱 /// </summary> public string Email { get; set; } [Required] /// <summary> /// 用户状态(0:禁用;1:启用) /// </summary> public int Status { get; set; } }View Code
以下是数据上下文类:
public class SAUnitOfWork : DbContext, IQueryableUnitOfWork { public DbSet<Operation> Operation { get; set; } public DbSet<PurchaseDetail> PurchaseDetail { get; set; } public DbSet<PurchaseHeader> PurchaseHeader { get; set; } public DbSet<Role> Role { get; set; } public DbSet<RoleOperations> RoleOperations { get; set; } public DbSet<SettlementDetail> SettlementDetail { get; set; } public DbSet<SettlementHeader> SettlementHeader { get; set; } public DbSet<User> User { get; set; } public DbSet<UserRoles> UserRoles { get; set; } }View Code
在VS菜单栏“工具”→“库程序包管理器”→打开“程序包管理器控制台”:
1.启用 Code First 迁移命令:Enable-Migrations
2.为迁移搭建基架:Add-Migration Initial
3.开始迁移:Update-Database
执行完以上三个命令后,你会发现在你的数据库服务实例中已经生成了数据库: