EntityFramework6.0 Reading Notes [2] ----- Basic Object Data Model [Top], entityframework6
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. For
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 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 of
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.
2 Reading Notes
Several articles:
Camel Xiangzi tells the tragic story of a rickshaw Xiangzi in the old Beijing city of China. Xiangzi came to the city, eager to work honestly to create life. With his belief in buying a car, he tried his best to make money, just like a small spinning top. Finally, Xiang Zi got the car he dreamed of. For Xiang Zi, he didn't know how many pairs he had worn. But the fate of people, the car was taken one after another, Xiangzi's dream fire went out again and again. However, Xiangzi still refused to give up and kept fighting again. Here, I can't help but be touched and pity. I am touched by the tenacity of Xiangzi's perseverance and hard work for the dream. I am tortured by the miserable fate of Xiangzi, however, you can feel pity in frustration and disappointment. This taught me to be strong in the face of difficulties and stand up on your own if you fail. Later, from the combination with Tiger girl to the final death of Tiger girl, Xiangzi's mind was deeply affected. In the end, the car was sold, and the Tiger girl died. Everything turned to nothing, and it was just like the beginning. Everything is like wiping pen marks with an eraser. It will evaporate everything, leaving only a few deep marks. In Xiangzi's heart, deep marks are always branded. Xiangzi was so hostile to the world that he began to avenge everyone around him. In the past, Xiangzi, who spoke about righteousness, began to cheat his friends and use them to steal everything from them. He became treacherous and even shameless. He has changed a person, and he has done everything as long as he can get the money. I can't help but feel sad, disappointed, and angry. We are disappointed that Xiangzi did not stick to it and was eventually swallowed up by the darkness. The angry thing is that the honest and honest Xiangzi has done everything to hurt the truth,
He had no regrets. Xiang Zi's tragedy is the product of his social life environment. In a dark society, the power of mankind is too small. Xiang Zi tried to defeat his fate multiple times, but in the end, his mind and body were bruised again. Xiang Zi struggled again and again in pain, and gradually became distorted and swallowed up by the darkness. In the past, I was ambitious and full of bones. Now I only have to read money. Under the temptation of darkness and money in society,
Xiangzi has no backbone. "Money will introduce people into a bad society, put aside noble ideals, and be willing to go to hell ." Indeed, Xiangzi strives for money for "life". In "life" and "ideal", he chooses "life" because "life" is the only thing that the poor can choose. At that time, the life of the poor may be like the two leaders of the jujube kernel-don't starve to death when you are young; fortunately, it is very difficult to starve to death when you are old. At this time, we realized that the fate of people is not completely controlled by ourselves. Xiangzi, the hero of the story, struggled with his life with a tough character and Persistent attitude. But in the end, his fate still ruined Xiangzi. In a dark society, human nature becomes distorted, and people are filled with hatred ......
Everyone has read the Water Margin. Does anyone find that this is a hero novel, while the Romance of the Three Kingdoms is a historical romance.
Moreover, Shi Nai has made a lot of adaptations to the hacker, which was originally written at the center of "Liang shanpo Notepad". However, Shi Nai pays more attention to the fate of heroes, several of the important figures have personal biographies. The author concatenates them in the form of "column transfer. However, Shi Nai did not completely forget the historical events. Before the "Heroes of Liangshan Park" event, there was a historical event "three dozen Zhu jiazhuang. After the "Heroes of Liangshan Park" incident, I wrote another incident, such as Zhao 'an, and there was no independent personal biography.
In my opinion, it is wrong for Song Jiang to be promoted as the boss because of the following reasons:
1. He won't do the martial arts himself. You know which hero won't do the martial arts at the time. But what about Song Jiang? It's just a scholar with no help.
2. He was brave and helpless. During daily battles, most of his tactics were intended by Military Division Wu, and he did not do much work.
3. he did a very wrong thing, that is, Zhao 'an. after being recruited, I was very sad to be separated from my brother. after being enlightened by the Court, several people were taken together to die.
The Water Margin is different from the Romance of the Three Kingdoms, which can be seen from the historical data.
The Romance of the Three Kingdoms was cited by Chen Shou's Three Kingdoms and Pei songzhi's Three Kingdoms. The Song Jiang uprising described in the Water Margin has been recorded in the books of Song Shi, but they are very simple. It can be noted that the author has written many short stories about small... the remaining full text>
20 Reading Notes and 20 reading diaries each
Thoughts on reading fate
A few days ago, I bought a book In Shucheng, called "dripping water, hiding the Sea". There are 300 classic philosophical stories in it. Now I will taste a little story called destiny.
Destiny refers to the fate of a single child. One is Divination by a monk as "champion" and the other is "beggar ". Two decades later, the original "champion" became a beggar, while the "beggar" became a "champion ".
God said: "I give everyone 1/3 of his fate, and the rest is how to grasp it ."
After reading this passage, I was very touched. There are many simple words to grasp and grasp fate, but how many people really grasp their own destiny? You don't have to blame your talent, let alone your own destiny, because fate is in your own hands and you can change it at any time! If you want
Reading "never mind weapons"
A person who has said goodbye to weapons is either a prisoner of the enemy or a prisoner of love. I am not very good at self-protection. I am really a person who gives up self-protection. just like the database of life, you can open all programs and read all files at any time without entering the password. what I am talking about is a prisoner in this sense. when I put myself in the sun, I realized that there was no disguise, and the days of concealment were disturbing. when I realized that the resistance was helpless, how much time was irretrievable, and how much memory gradually fades out from my heart. after all, a prisoner is a person who cannot resist harm, that is, to have enough courage to give up hope and must bear all the pressure to survive. originally, in a personal space, you can immerse yourself in your own fantasies, and you can create virtual flowers from the dust. a person who gives up self-protection cannot cheat himself, only by constantly purifying the inner world.
After reading the paper of hampir
Shakespeare's "Hamlet" is a classic masterpiece. on the surface, this book is not much different from historical legends. It tells the story of Prince Denmark's revenge for his father, which is full of bloody violence and death. as
Horazu, a Chinese character in the drama, said: you can hear rape and kill, unusual repairs, decisions in the dark, accidental slaughter, tactics of killing by hand, and self-harm ending. the twists and turns of the plot, closely around revenge
Expand. he hurried back to China from wandunburg, Germany to attend his father's funeral. What makes him unacceptable was that he did not catch up with his father's funeral, he witnessed the wedding of his mother and uncle kraudis, which had made hamwright suspect. In addition, at night, he met his father's dead soul on the terrace of the palace Castle and mourned, this violent behavior was done by Uncle James and asked him to avenge his father. so far, he began a difficult revenge process, and began a battle with kradis. in the end, the sword of revenge was issued to kradiss.
Count of Christchurch
Love is complete, hate is thorough. the reward is complete, and the revenge is complete. this is what I feel most after reading "Revenge of Christchurch. there is a saying in China that revenge of a gentleman is not too late for ten years. Revenge also needs to be refined and powerful, and it is not just a matter of time. the Count of Christchurch explained the saying most specifically with his own actions. after 14 years of his career in the dungeon, his life is to find his loved ones, beneents, and enemies. after confirming the person we are looking for, he did not go through the fire as we have seen in martial arts novels. he chose his own method. for the ship owner who once had favor with him, he did his best and silently supported him. In various ways, he never let them know that he was actually here for good. if his reward is touching, his revenge is so full that we are a little surprised when we have a few bits.
Interpretation of dreams
Freuch (1856 -- l939) is a famous Austrian psychologist and founder of the School of spiritual analysis. his work spans half a century and has a wide and profound influence on literature, philosophy, theology, ethics, aesthetics, political science, sociology, and mass psychology, if we use the scope of the impact as a measure of greatness, then freusi is undoubtedly the greatest psychologist. in the history of human thoughts, freuch initiated another revolution of the neighborhood. He pointed out that the human consciousness cannot be controlled by consciousness, and the human subconscious contains a huge psychological content, in his most rational voice, he told us about the irrational nature of mankind. interpretation of dreams is Frolo ...... remaining full text>