前言:之前在下總結編寫了一篇 EF框架搭建小總結--ModelFirst模型優先 博文,看到一段時間內該博文的訪問量蹭、蹭蹭、蹭蹭蹭...往上漲(實際也不是很多,嘿嘿),但是還是按捺不住內心的喜悅(蠻有成就感的),感覺為大家做了一點點小小的貢獻,在下也就再接再厲(趁風大,再浪浪),總結一篇 CodeFirst代碼優先的博文,若有不當之處,還望大家斧正。
Code First介紹: Code First模式是一種很cool的模式,手動創建POCO(全稱Plain Old Class Object,也就是最基本的CLR Class,實體類)類,數據層DbContext及映射關系,通過Database.SetInitializer(本次采用dbcontext.Database.CreateIfNotExists方法)生成數據庫,自動生成方便快速、更易維護、非常靈活。
使用工具: win7操作系統,vs2012, sqlserver2008
開始
1.創建新項目
2.創建完項目后,需要引用"EntityFramework"程序包,且看如何引入
2.1 在【引用】上單擊右鍵,選中【管理NuGet程序包(N)】
2.2 在彈出的窗口上選擇【聯機】,找到【EntityFramework】程序包,點擊安裝
2.3 安裝后的效果
3 開始上代碼
3.1 創建一個User實體類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CodeFirst { public class User { public int Id { set; get; } public string Name { set; get; } public string Password { set; get; } } }
3.2 創建連接上下文的類,該類的一些說明在代碼里已標注
using System; using System.Collections.Generic; using System.Data.Entity;// DbContext類需要引入的類庫 using System.Linq; using System.Text; using System.Threading.Tasks; namespace CodeFirst { //該類繼承於DbContext類, //DbContext是EntityFramework很重要的部分,連接類與數據庫的橋梁,是與數據庫通信的主要類 public class CodeFirstDbContext : DbContext { //構造函數 public CodeFirstDbContext() : base("name = CodeFirstDbContext")//根據配置文件中鏈接數據庫 //CodeFirstDbContext會在app.config中進行配置 { } public DbSet<User> User { get; set; } } }
3.3 在配置文件App.config中添加內容
<?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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<!--連接本地sql server數據庫需要配置的文件-->
<connectionStrings>
<add name="CodeFirstDbContext" connectionString="Data Source=.;Initial Catalog=DatabaseName;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
其中Data Source = . 表示連接的是本地數據庫,Initial Catalog=DatabaseName;表示一會創建的數據庫名字為DatabaseName
3.4 在主程序中創建數據庫,數據表,並填入字段
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CodeFirst { class Program { static void Main(string[] args) { //創建上下文 CodeFirstDbContext dbcontext = new CodeFirstDbContext(); //創建數據庫 dbcontext.Database.CreateIfNotExists(); //創建表,並將字段加入進去 User u = new User(); u.Name = "li"; u.Id = 1; u.Password = "123456"; //將實體賦予上下文,並添加到表里 dbcontext.User.Add(u); //保存 dbcontext.SaveChanges(); Console.WriteLine("成功"); Console.ReadKey(); } } }
運行主程序后,便會在數據庫中成功創建你的數據庫,數據表。
結果: