. Net EntityFramwork6.0 EF framework development entry,
I. EnvironmentDevelopment Environment: Sqlserver2008 R2, Visual Studio20122. Preparations1. Create an empty MVC project. 2. Obtain the EntityFramework package through NuGet. Operations: 3,Code Implementation1. Create an object class
[Table ("User")] public class User {[Display (Name = "id")] public int Id {get; set ;} [Display (Name = "UserName")] [Required (ErrorMessage = "UserName Required")] public string UserName {get; set ;} [Display (Name = "Password")] public string Password {get; set ;}}
2. Construct database login Class a), inherit DbContext B), create database connection string, string name and a) the name of the Step class is consistent or inconsistent (you must specify the database access string)
Public class DemoConnectionContext: DbContext {public DbSet <User> dbuser {get {return Set <User> () ;}} public DemoConnectionContext (): base ("name = DemoConnectionContext ") // specify the Database access string {} static DemoConnectionContext () {Database. setInitializer <DemoConnectionContext> (new DropCreateDatabaseIfModelChanges <DemoConnectionContext> ());}}
3. Database Operations
// Declare database operations
DemoConnectionContext db = new DemoConnectionContext ();
A) add data
User user = new Models.User{UserName = "jay", Password = "123"};db.dbuser.Add(user);db.SaveChanges();
B) query data
// Specify the ID to query var userObj = db. dbuser. find (1); // Write A var userObj = db. dbuser. where (u => u. id = 1 ). firstOrDefault (); // method 2 // method 3 var userObj = (from u in db. dbuser where u. id = 1 select u ). toList ();
C) update data
// Update table var userObj = (from u in db. dbuser where u. userName = "jay" select u ). toList (); userObj [0]. password = "1234"; db. dbuser. attach (userObj [0]); // update the specified column var setEntry = (IObjectContextAdapter) db ). objectContext. objectStateManager. getObjectStateEntry (userObj [0]); setEntry. setModifiedProperty ("Password"); db. saveChanges ();
D) delete data
// Delete data by specifying the ID User userObj = new User {Id = 1}; db. dbuser. attach (userObj); // append object db. dbuser. remove (userObj); // Delete the object database. saveChanges ();
E). Operations on multiple tables are put in TransactionScope.
Try {// multi-Table transaction operations. Put the operations in TransactionScope using (var trac = new TransactionScope () {User user = new User {UserName = "jack ", password = "1234"}; db. dbuser. add (user); // throw new Exception ("throw Exception"); user = new User {UserName = "David", Password = "1234"}; db. dbuser. add (user); db. saveChanges (); trac. complete (); // transaction commit. If this method is not executed, the transaction will be rolled back automatically.} catch (Exception ex) {throw ;}
F :) T-SQL Query
1) SQL query, entity class
// SQL query, entity class var userList = db. Database. SqlQuery <User> ("select id, UserName, Password from [User]"). ToList ();
2) specify an object to query an object
var userList= db.dbuser.SqlQuery("select id,UserName,Password from [User]").ToList();
3) Execute SQL commands
db.Database.ExecuteSqlCommand("update [User] set UserName='Michel' where UserName='jay'");
FAQs1. error message: The model backing the 'democonnectioncontext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink? LinkId = 238269). The error message generally indicates that the database information must be updated and the object information must be consistent with the object information because the object attributes, links, and names change. Solution: 1. back up the database and shut down all database operations. The EF framework automatically updates the script (normally, the database is deleted and re-created. If the database is already in use and you do not want to delete the existing data, it is not recommended to run the script) 2. specifies whether to synchronously update the Database. It can be set to Database. setInitializer <DemoConnectionContext> (null);, manually update the database to be consistent with the object class