Entity Framework6学习笔记(一)
技術(shù)的發(fā)展真是太快了,轉(zhuǎn)眼間已經(jīng)是第6個(gè)版本,現(xiàn)在抓緊時(shí)間學(xué)習(xí)一下!進(jìn)入正題……
前提條件:
1.聯(lián)網(wǎng)
2.平臺(tái):在Visual Studio 2013
3.數(shù)據(jù)庫(kù):SQL Server 2008R2
一、在項(xiàng)目中引入Entity Framework框架支持:
1.檢查在項(xiàng)目中有沒(méi)有引用EntityFramework和EntityFramework.SqlServer庫(kù)文件;
2.如果沒(méi)有則點(diǎn)擊工具—NuGet程序包管理器—管理解決方案的NuGet程序包…
3.打開(kāi)程序包管理界面搜索entity(如果沒(méi)有安裝過(guò)會(huì)顯示“安裝”按鈕)點(diǎn)擊安裝即可
二、初步使用:
1.配置數(shù)據(jù)庫(kù)連接
<connectionStrings><add name="mydb" connectionString="Data Source=機(jī)器名\數(shù)據(jù)庫(kù)服務(wù)名;Initial Catalog=abcc;User ID=sa;Password=123456" providerName="System.Data.SqlClient"/></connectionStrings>?
2.新建一個(gè)EntityDB類并繼承DbContext
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Data.Entity; 7 using System.Data.Entity.ModelConfiguration; 8 namespace ConsoleEntity 9 { 10 public class EntityDB:DbContext 11 { 12 public EntityDB(): base("mydb") 13 { 14 15 } 16 public virtual DbSet<Person> Person{ get; set; } 17 18 19 20 } 21 public class Person 22 { 23 public int Id{get;set;} 24 public string Name{get;set;} 25 } 26 27 } View Code3.插入數(shù)據(jù)測(cè)試是否成功:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 Database.SetInitializer(new CreateDatabaseIfNotExists<EntityDB>()); 7 EntityDB db = new EntityDB(); 8 db.Person.Add(new Person { Id = 1, Name = "ddd" }); 9 db.SaveChanges();10 var d = db.Person.ToList(); 11 foreach(var t in d) 12 Console.WriteLine(t.Name); 13 Console.ReadKey(); 14 } 15 }
如果屏幕出現(xiàn)“ddd”就是成功了,下次將講解數(shù)據(jù)庫(kù)生成策略!
轉(zhuǎn)載于:https://www.cnblogs.com/bhdblogs/p/4837423.html
總結(jié)
以上是生活随笔為你收集整理的Entity Framework6学习笔记(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Kendo UI Professiona
- 下一篇: 附5、MDT 2013 Update 1