In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "Entity Framework how to use Code First mode to manage database", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Entity Framework how to use Code First schema to manage database" this article.
Managing database connections 1. Conventions for managing connections using configuration files
In the database context class, if we only inherit the parameterless DbContext and create a connection string with the same name as the database context class in the configuration file, then EF will use the connection string to automatically calculate the location and name of the database. For example, our database context is defined as follows:
Using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConventionConfigure.EF {/ inherit DbContext / public class SampleDbEntities with no parameters: DbContext {public SampleDbEntities () {/ / create a database Database.CreateIfNotExists () when the database does not exist;}
The connection string defined in the configuration file is as follows:
The value of name in the defined connection string is the same as the class name of the database context class created, so EF uses the connection string to perform database operations. What happens?
To run the program, the Program class is defined as follows:
Using ConventionConfigure.EF;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConventionConfigure {class Program {static void Main (string [] args) {using (var context = new SampleDbEntities ()) {} Console.WriteLine ("created successfully"); Console.ReadKey ();}
When running the application, EF looks for our database context class, "SampleDbEntities", and looks for a connection string with the same name in the configuration file, and then it uses the connection string to calculate which database provider should be used, then checks the database location, and then creates a database file named TestDb.mdf in the specified location. At the same time, a database named TestDb is created based on the Initial Catalog attribute of the connection string. The database structure created is as follows:
Looking at the created database, you will find that there is only one migration record table.
2. Use the existing ConnectionString
If we already have a ConnectionString that defines the location and name of the database, and we want to use this connection string in the database context class, the connection string is as follows:
Take the database TestDb created above as the existing database, add the entity class Student, and use the existing ConnectionString to query the Student table of the database. The Student entity class is defined as follows:
Using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ExistsConnectionString.Model {[Table ("Student")] public class Student {public int Id {get; set;} public string Name {get; set;} public string Sex {get; set;} public int Age {get; set;}}
We pass the name of the connection string into the parametric constructor of the database context DbContext. The database context class is defined as follows:
Using ExistsConnectionString.Model;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ExistsConnectionString.EF {public class SampleDbEntities: DbContext {public SampleDbEntities (): base ("name=AppConnection") {} / / add public virtual DbSet Students {get; set;}} to the data context
The above code passes the name of the connection string to the parameterized constructor of the DbContext class, so that our database context starts using the connection string, outputting the values of the Name and Age fields in the Program class:
Using ExistsConnectionString.EF;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Namespace ExistsConnectionString {class Program {static void Main (string [] args) {using (var context = new SampleDbEntities ()) {foreach (var item in context.Students) {Console.WriteLine ("name:" + item.Name+ "" + "Age:" + item.Age) }}}
Run the program and the following error will be reported:
The reason for the above error is that the database context has changed and does not match the existing database. Solution:
Delete or rename the migration record table in the database.
Rerun the program, and the result is as follows:
Note: if there is a ConnectionString with the same name as the database context class in the configuration file, the connection string with the same name will be used. It doesn't matter how we change the name of the incoming connection string, which means that the connection string with the same name as the database context class takes precedence. (that is, the contract is greater than the configuration)
3. Use an existing connection
Usually in some old projects, we only use EF Code First in some part of the project, and we want to use an existing database connection for the data context class. To achieve this, you can pass the connection object to the constructor of the DbContext class. The data context is defined as follows:
Using ExistsDbConnection.Model;using System;using System.Collections.Generic;using System.Data.Common;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ExistsDbConnection.EF {public class SampleDbEntities: DbContext {public SampleDbEntities (DbConnection con): base (con, contextOwnsConnection: false) {} public virtual DbSet Students {get; set;}}
Note here that the contextOwnsConnection parameter is passed into the context as a false because it is passed in from the outside, and when the context is out of scope, someone may want to use the connection. If true is passed in, the database connection will be closed as soon as the context goes out of scope.
The Program class is defined as follows:
Using System;using System.Collections.Generic;using System.Data.Common;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Configuration;using ExistsDbConnection.EF;namespace ExistsDbConnection {class Program {static void Main (string [] args) {/ / read the connection string string conn = ConfigurationManager.ConnectionStrings ["AppConnection"] .ConnectionString / / DbConnection is an abstract class and cannot be instantiated directly. Declare that the subclass points to the parent class object DbConnection con = new SqlConnection (conn). Using (var context = new SampleDbEntities (con)) {foreach (var item in context.Students) {Console.WriteLine ("name:" + item.Name + "" + "Age:" + item.Age);} Console.WriteLine ("read complete") Console.ReadKey ();}
Run the program and the result is as follows:
Manage the creation of the database
When you run the EF Code First application for the first time, EF does the following:
1. Check the DbContext class that you are using.
2. Find the connectionString used by the context class.
3. Find the domain entity and extract the information related to the pattern.
4. Create a database.
5. Insert the data into the system.
Once the schema information is extracted, EF uses the database initializer to push the schema information to the database. There are many possible strategies for database initializers, and the default policy for EF is to recreate if the database does not exist, or to use the existing database if it exists. Of course, we may sometimes need to override the default policy, and the database initialization strategy we may use is as follows:
CreateDatabaseIfNotExists:CreateDatabaseIfNotExists: as the name implies, if the database does not exist, then recreate it, otherwise use the existing database. If the schema information extracted from the domain model does not match the actual database schema, an exception is thrown.
DropCreateDatabaseAlways: if this strategy is used, the database will be destroyed each time the program is run. This is often useful in the early stages of the development cycle (such as when designing domain entities) and from a unit testing perspective.
DropCreateDatabaseIfModelChanges: this strategy means that if the domain model changes (specifically, the schema information extracted from the domain entity does not match the actual database schema information), the previous database (if any) is destroyed and a new database is created.
MigrateDatabaseToLatestVersion: if this initializer is used, EF automatically updates the database schema whenever the entity model is updated. It is important here that this strategy updates the database schema without losing data or updating existing database objects in an existing database. MigrateDatabaseToLatestVersion initializers are available only from EF4.3.
1. Set initialization policy
EF uses CreateDatabaseIfNotExists as the default initializer by default. If you want to override this policy, you need to use the Database.SetInitializer method in the constructor in the DbContext class. The following example uses the DropCreateDatabaseIfModelChanges policy to override the default policy. The database context class is defined as follows:
Using InitializationStrategy.Model;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InitializationStrategy.EF {public class SampleDbEntities: DbContext {public SampleDbEntities (): base ("name=AppConnection") {/ / override the default policy Database.SetInitializer (new DropCreateDatabaseIfModelChanges ()) with the DropCreateDatabaseIfModelChanges policy } / / add public virtual DbSet Students {get; set;}} to the data context
This way, whenever a context class is created, the Database.SetInitializer () method is called and the database initialization policy is set to DropCreateDatabaseIfModelChanges.
Two new attributes, Email and Address, are added to the Student domain entity class:
Using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InitializationStrategy.Model {[Table ("Student")] public class Student {public int Id {get; set;} public string Name {get; set;} public string Sex {get; set;} public int Age {get; set;} public string Email {get; set } public string Address {get; set;}
The Program class is defined as follows:
Using InitializationStrategy.EF;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Namespace InitializationStrategy {class Program {static void Main (string [] args) {using (var context = new SampleDbEntities ()) {foreach (var item in context.Students) {} Console.WriteLine ("created successfully"); Console.ReadKey ();}
After running the program, the database table structure is as follows:
Note: if we are in a production environment, then we certainly do not want to lose existing data. At this point, we need to turn off the initializer, just pass the null to the Database.SetInitlalizer () method, as follows:
Public SampleDbEntities (): base ("name=AppConnection") {Database.SetInitializer (null);} 2, fill seed data
So far, no matter which strategy we choose to initialize the database, the resulting database is an empty database. But in many cases we always want to insert some data after the database is created and before using it for the first time. In addition, the development phase may want to populate some data as admin, or to falsify some data to test how the application performs in a particular scenario.
When we use DropCreateDatabaseAlways and DropCreateDatabaseIfModelChanges initialization strategies, inserting seed data is important because the database is recreated every time the application is run, and manually inserting data after each database creation is tedious. Let's take a look at how to use EF to insert seed data after the database is created.
To insert some initialization data into the database, we need to create a database initializer class that meets the following conditions:
1. Derive data from existing database initializer classes.
2. Seeding during database creation.
The following shows how to initialize seed data
1. Define domain entity class using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InitializationSeed.Model {[Table ("Employee")] public class Employee {public int EmployeeId {get; set;} public string FirstName {get; set;} public string LastName {get; set;}} 2, create database context
Use the Code First method of EF to create a database context for the above model:
Public class SampleDbEntities: DbContext {public virtual DbSet Employees {get; set;}} 3, create database initializer classes
Assuming that we are using the DropCreateDatabaseAlways database initialization strategy, the initializer class inherits from the generic class and passes in the database context as the type parameter. Next, to seed the database, you override the Seed () method of the DropCreateDatabaseAlways class, and the Seed () method gets the database context, so we can use it to insert data into the database:
Using InitializationSeed.Model;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks Namespace InitializationSeed.EF {/ Database initializer class / public class SeedingDataInitializer: DropCreateDatabaseAlways {/ override the Seed method of DropCreateDatabaseAlways / protected override void Seed (SampleDbEntities context) {for (int I = 0; I < 6) ) {var employee = new Employee {FirstName= "Test" + (iTun1), LastName= "engineer"}; context.Employees.Add (employee);} base.Seed (context);}
The above code creates six Employee objects through the for loop and adds them to the Employees collection property of the database context class. It is worth noting that we did not call the DbContext.SaveChanges () method because it is called automatically in the base class.
4. Use the database initializer class to ask about the database class using InitializationSeed.Model;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InitializationSeed.EF {public class SampleDbEntities: DbContext {public SampleDbEntities (): base ("name=AppConnection") {/ / Type pass SeedingDataInitializer Database.SetInitializer (new SeedingDataInitializer ()) } / / Domain entities are added to the data context public virtual DbSet Employees {get; set;}} 5. Access the database using InitializationSeed.EF;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks in the Main method Namespace InitializationSeed {class Program {static void Main (string [] args) {using (var context = new SampleDbEntities ()) {foreach (var item in context.Employees) {Console.WriteLine ("FirstName:" + item.FirstName+ "+" LastName: "+ item.LastName) }} Console.WriteLine ("read complete"); Console.ReadKey ();} 6. Run the program to view the results
View the database
Seed data filling is complete.
7. Use data migration to populate seed data
The Configuration class is generated by using data migration. The Configuration class is defined as follows:
Namespace DataMigration.Migrations {using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; internal sealed class Configuration: DbMigrationsConfiguration {public Configuration () {AutomaticMigrationsEnabled = false;} protected override void Seed (DataMigration.SampleDbEntities context) {/ / This method will be called after migrating to the latest version. / / You can use the DbSet.AddOrUpdate () helper extension method / / to avoid creating duplicate seed data. }}}
Overriding the Seed () method of the Configuration class can also insert seed data and override the Seed () method:
Namespace DataMigration.Migrations {using DataMigration.Model; using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; internal sealed class Configuration: DbMigrationsConfiguration {public Configuration () {AutomaticMigrationsEnabled = false;} protected override void Seed (DataMigration.SampleDbEntities context) {/ / This method will be called after migrating to the latest version. / / You can use the DbSet.AddOrUpdate () helper extension method / / to avoid creating duplicate seed data. Context.Employees.AddOrUpdate (new Employee {FirstName = "Test 1", LastName = "engineer"}, new Employee {FirstName = "Test 2", LastName = "engineer");}
Use data migration, and then view the database results:
It is found that the use of data migration also inserts seed data into the database.
These are all the contents of the article "how Entity Framework uses Code First Schema to manage databases". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.