Model First is called "Model First". The Model here refers to "ADO. NET Entity Framework Data Model ". At this time, your application does not design related databases. in Visual Studio, we generate databases and Data classes by designing the Data Model.
Step 1: First add a simple console application, and then add an ADO. NET object data model to this project.
Step 2: select an empty model.
Step 3: Add a new object to the model design view
Click OK.
Step 4: Add two attributes to the Order object. The Customer type is string, and the OrderDate type is DateTime.
Set the field type, right-click the current field ==>>> property, and find the type setting.
Step 5: add another entity OrderDetail order details, Product
Step 6: add the relationship between the two. "Order" and "OrderDetail" are one-to-many relationships. "Order" can access the "OrderDetail" entity through the "OrderDetails" attribute, "OrderDetail" can access the "Order" entity through the "Order" attribute and add a foreign key constraint to "OrderDetail ".
Click OK. The added link is
So far, the Model in Model First has been created and needs to be generated to the database. In the Model design view, select "generate to database according to Model ..." :
Make sure that a blank database already exists in the database.
Step 7: select and add a data link
Click Next and you will see the following interface
Click Finish to view the generated SQL script.
Then, right-click the SQL script and click Execute
The database has two more tables.
class Program { static void Main(string[] args) { using (var db = new ModelTestContainer()) { 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(); } }
Running effect:
After running twice, add two pieces of data.
Note:: If our model changes, you only need to modify the model in the model design view and save the changes to the object class, then select "from model generation to Database" to re-execute the generated script.