Preface
In this article, we will demonstrate the basic modeling of enitityframework [modeling is also the core feature of entityframework] examples, such as entity separation and inheritance. We started to demonstrate how to create a simple conceptual model and then let enitityframework build the underlying database. In the remaining examples, we will show you how to create a model from an existing table and database relationship.
Create a simple model
1. Click Add new item, select ADO. Net Object Model under data, and select empty model.
2. Right-click and select Add object
3. Name the object as person, the object set as people, and add a key attribute named ID and type as int32.
4. Add a scalar property
5. Add the scalar attribute firstname. lastname, middlename, phonenumber, and specify the database generation policy for the key attribute ID.
6. Right-click the blank space --> properties, modify the object container name to ef6recipescontext, and the database architecture name to Article2. These are meaningful actions to better manage the model.
7. Right-click the database generated based on the model, select create new connection, and select the target database.
8. Generate a warehouse model in the. edmx file and run the database script.
How does it work?
using System;namespace EntityFrameworkDemo{ class Program { static void Main() { using (var context=new EF6RecipesContext()) { var person = new Person { FirstName = "Robert", MiddleName = "Allen", LastName = "Doe", PhoneNumber = "867-5309" }; context.People.Add(person); person = new Person { FirstName = "John", MiddleName = "K.", LastName = "Smith", PhoneNumber = "824-3031" }; context.People.Add(person); person = new Person { FirstName = "Billy", MiddleName = "Albert", LastName = "Minor", PhoneNumber = "907-2212" }; context.People.Add(person); person = new Person { FirstName = "Kathy", MiddleName = "Anne", LastName = "Ryan", PhoneNumber = "722-0038" }; context.People.Add(person); context.SaveChanges(); } using (var context=new EF6RecipesContext()) { foreach (var person in context.People) { Console.WriteLine("{0} {1} {2}, Phone: {3}", person.FirstName, person.MiddleName, person.LastName, person.PhoneNumber); } } Console.ReadKey(); } }}
Running Effect
As for the benefits of using, we will not talk about it here, because it is also the most basic. The following is an explanation in the book. If you are not familiar with it, take a look.
There are a few nice features of using () statements. First, when the Code Execution leaves the using () {} block,
The dispose () method on the context will be called because dbcontext implements the idisposable interface.
Dbcontext, the dispose () method closes any active database connections and properly cleans up any other resources
That need to be released.
Second, no matter how the Code leaves the using () {} block, the dispose () method is called. Most importantly,
This includes des return statements and exceptions that may be thrown within the code block. The using () {} block is kind
Of a guarantee that critical resources will be reclaimed properly.
The best practice here is always to wrap your code in the using () {} block when creating new instances
Dbcontext. It's one more step to help bulletproof your code
Generate a model from an existing database
Problem
The table in an existing database has some views and foreign key constraints. you want to create a model for this database.
Solution
The structure in your database may be as follows:
First, install the previous steps. Open the wizard and select the tables and views to be operated from the database.
After clicking finish, entityframework will deduce the one-to-many relationship between poet and poem and between meter and poem.
From the Model Browser, we can see that a poem corresponds to an author and a category, corresponding to the poet and meter navigation attributes. If we have a poem entity, then the navigation attribute poet will also contain a collection of the poet's entities [because it is a one-to-many relationship], as is meter. Because sqlserver does not support defining relationships on views, vwliberary is listed as a group of empty navigation attributes.
How does it work?
using (var context = new EF6RecipesEntities()) { var poet = new Poet {FirstName = "John", LastName = "Milton"}; var poem = new Poem {Title = "Paradise Lost"}; var meter = new Meter {MeterName = "Iambic Pentameter"}; poem.Meter = meter; poem.Poet = poet; context.Poems.Add(poem); poem = new Poem {Title = "Paradise Regained", Meter = meter, Poet = poet}; context.Poems.Add(poem); poet = new Poet {FirstName = "Lewis", LastName = "Carroll"}; poem = new Poem {Title = "The Hunting of the Shark"}; meter = new Meter {MeterName = "Anapestic Tetrameter"}; poem.Meter = meter; poem.Poet = poet; context.Poems.Add(poem); poet = new Poet {FirstName = "Lord", LastName = "Byron"}; poem = new Poem {Title = "Don Juan", Meter = meter, Poet = poet}; context.Poems.Add(poem); context.SaveChanges(); } using (var context = new EF6RecipesEntities()) { var poets = context.Poets; foreach (var poet in poets) { Console.WriteLine("{0} {1}", poet.FirstName, poet.LastName); foreach (var poem in poet.Poems) { Console.WriteLine("\t{0} ({1})", poem.Title, poem.Meter.MeterName); } } } // using our vwLibrary view using (var context = new EF6RecipesEntities()) { var items = context.vwLibraries; foreach (var item in items) { Console.WriteLine("{0} {1}", item.FirstName, item.LastName); Console.WriteLine("\t{0} ({1})", item.Title, item.MeterName); } }
Running Effect
We used sqlserver profile to monitor the execution of this code and found that VaR poets = context was not executed. vwlibraries; the system immediately captures data from the database, and stores the results in the memory after executing the foreach statement. The data is directly read from the database without going through the database, in short, it can be considered to be executed only when it is used. The detailed process will be summarized later.
Entityframework6.0 Reading Notes [2] ----- Basic Object Data Model [1]