The Code First mode is called the "Code First" mode. It is a new function from EF4.1. When using the Code First mode for EF development, developers only need to write the corresponding data classes (in fact, the implementation process of the domain model), and then automatically generate the database. The benefit of this design is that we can perform all the data operations on the conceptual model without having to store the data, this allows us to develop data-oriented applications in an object-oriented manner.
From a certain perspective, the difference between "Code First" and "Model First" is not very obvious, but it does not rely on the Entity Data Model Designer, but directly through encoding (data class) method to Design the entity model (this is why "Code First" was First called "Code Only ). However, the processing process of EF is different. For example, if we use Code First, we no longer need EDM files. All mappings are mapped and configured through "Data annotation" and "fluent API. In addition, "Code First" does not necessarily mean that models must be defined through data classes. In fact, data classes can also be generated through existing databases.
Step 1: Add a console application, and then add two simple entity classes in this project.
ID { ; Product { ; OrderID { ; Order Order { ;
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeFirstTest{ public class Order { public int ID { get; set; } public string Customer { get; set; } public DateTime OrderDate { get; set; } public virtual List<OrderDetail> OrderDetails { get; set; } }}
With these two classes, Let's define a database context. With them, we can add, delete, modify, and query data. This class must inherit from "System. data. entity. dbContext class to grant it the data operation capability. Therefore, we need to install the EntityFramework package for this application, because so far we have not introduced any content related to the EF framework, and we need to introduce the EF-related assembly. But we have a better choice: NuGet. Online installation through NuGet: Right-click the project and choose "Manage NuGet Packages ..."; Select Online, select EntityFramework, and click Install. If you are not familiar with NuGet, click here to see how to use NuGet to manage the project library.
Database context operations:
namespace CodeFirstTest{ public class OrderTestContext:DbContext { public DbSet<Order> Orders { get;set;} public DbSet<OrderDetail> OrderDetails { get; set; } }}
static void Main(string[] args) { using (var db = new OrderTestContext()) { Order Order = new Order(); Order.Customer = "aehyok"; Order.OrderDate = DateTime.Now; db.Orders.Add(Order); db.SaveChanges(); IQueryable<Order> Orders = from Orderes in db.Orders select Orderes; foreach (Order O in Orders) { Console.WriteLine("OrderID is {0},Customer is {1}", O.ID, O.Customer); } } Console.ReadLine(); }
Call results
Here, we can find that we have not done any processing, no database creation, and no configuration for the database. Of course, it should have a default configuration.
Next, let's take a look at how to configure it.
We can add a database link string in the App configuration file.
<?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="CodeFirstTest" connectionString="Data Source=.;Database=CodeFirstTest;UID=sa;PWD=sa123;" providerName="System.Data.SqlClient"></add> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> </entityFramework></configuration>
It mainly adds a database link string
Changes to database context operations
public class OrderTestContext:DbContext { public OrderTestContext(string connectionName) : base(connectionName) { } public DbSet<Order> Orders { get;set;} public DbSet<OrderDetail> OrderDetails { get; set; } }
A constructor is added with a parameter. Then, the call is also adjusted.
using (var db = new OrderTestContext("CodeFirstTest")) { Order Order = new Order(); Order.Customer = "aehyok"; Order.OrderDate = DateTime.Now; db.Orders.Add(Order); db.SaveChanges(); IQueryable<Order> Orders = from Orderes in db.Orders select Orderes; foreach (Order O in Orders) { Console.WriteLine("OrderID is {0},Customer is {1}", O.ID, O.Customer); } }
It mainly targets the operational context of the instantiated database.
The call result is also
At the same time, I opened SQL Server and found the database that was just configured in the configuration file.