First, the Entity Framework (Entity FrameWork) Introduction
• referred to as EF
• relations with Asp.Net MVC and ADO.NET relations
• ADO.NET Entity FrameWork is based on Microsoft's ADO.NET developed out of the corresponding object-relational (O / R Mapping) solution, is called early ObjectSpace, the latest version is EF6.0 [CodeOnly function have been better stand by】
• Entity Framework Entity FrameWork ADO.NET is a set of data to support the development of technology-oriented software applications. Microsoft is an ORM framework.
Second, what is the O / R Mapping
• The broad: ORM refers to the conversion between the object model and object-oriented relational database interface.
• In a narrow sense, ORM can be considered, based on the data stored in a relational database, data access interface a virtual object-oriented. Ideally, based on such an object-oriented interface, a OO persistent object should not need to know implementation details of any relational database storing data.
EF simple demonstration
The first step: Right-click -> Add New Item -> ADO.NET Entity Data Model
Step two: Entity Data Model Wizard, you can default
The third step: New Connection
The fourth step: the hook (that contains sensitive data in the connection string), the next step
Step Five: Select models include those database tables, can be completed
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace EFDemoFirst 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //添加一个添加操作 14 //1、声明一个EF的上下文 15 DEMOEntities dbContext = new DEMOEntities(); 16 //2、声明一个实体 17 T_Seats seats = new T_Seats(); 18 seats.userName = "test"; 19 seats.pwdWord = "test1123"; 20 //3、告诉EF做一个插入操作 21 dbContext.T_Seats.Add(seats); 22 //4、告诉上下文,把实体的变化保存到数据库里面去 23 dbContext.SaveChanges(); 24 Console.Write("ok"); 25 Console.ReadKey(); 26 } 27 } 28 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace EFDemoFirst 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //1、声明一个EF的上下文 14 DEMOEntities dbContext = new DEMOEntities(); 15 //2、声明一个实体 16 T_Seats seats = new T_Seats(); 17 seats.userName = "testTest"; 18 seats.pwdWord = "123456 "; 19 seats.id = 4; 20 //3、告诉EF做更新操作 21 //System.Data.Entity.EntityState.Added:添加 22 //System.Data.Entity.EntityState.Deleted:删除 23 //System.Data.Entity.EntityState.Modified:修改 24 dbContext.Entry<T_Seats>(seats).State = System.Data.Entity.EntityState.Modified; 25 //4、告诉上下文,把实体的变化保存到数据库里面去 26 dbContext.SaveChanges(); //执行SQL脚本的地方 27 Console.Write("ok"); 28 Console.ReadKey(); 29 } 30 } 31 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace EFDemoFirst 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //1、声明一个EF的上下文 14 DEMOEntities dbContext = new DEMOEntities(); 15 ////2、声明一个实体 16 T_Seats seats = new T_Seats(); 17 seats.id = 4; //删除的WHERE条件,不加全部删除 18 //3、告诉ef做更新操作 19 //system.data.entity.entitystate.added:添加 20 //system.data.entity.entitystate.deleted:删除 21 //system.data.entity.entitystate.modified:修改 22 dbContext.Entry<T_Seats>(seats).State = System.Data.Entity.EntityState.Deleted; 23 //4、告诉上下文,把实体的变化保存到数据库里面去 24 dbContext.SaveChanges(); //执行SQL脚本的地方 25 Console.Write("ok"); 26 Console.ReadKey(); 27 } 28 } 29 }