First knowledge of EF CodeFirst and first knowledge of efcodefirst

Source: Internet
Author: User

First knowledge of EF CodeFirst and first knowledge of efcodefirst

With the development of EntityFramework, the previous three methods, {Code First, Model First, Database First} CodeFirst, basically got rid of the other two methods.

Has become the most popular programming model. We first write the code and then automatically generate the database. Programmers only need to concentrate on writing code object-oriented, instead of writing annoying SQL statements. Create a console project and add two classes to the EF library through Nuget
Public class Blog {public string ID {get; set;} public string BlogName {get; set;} public string BlogAuthor {get; set ;} public virtual List <Post> Posts {get; set;} // navigation attribute} public class Post {public string ID {get; set;} public string PostName {get; set ;} public string Content {get; set;} public virtual Blog BelongBlog {get; set;} // navigation attribute}
These two classes belong to a one-to-many relationship. A blog can have multiple blog posts. The core of EF is the DBContext class, which is responsible for the interaction between entities and databases. Therefore, a context class is added.
public class BlogContext : DbContext    {        private readonly static string CONNECTIONSTRING = "Data Source=.;Initial Catalog=EFBlog;User       ID=sa;Password=11111;Integrated Security=True;Pooling=False";        public BlogContext():base (CONNECTIONSTRING)        {        }        public DbSet < Blog> Blogs { get ; set ; }        public DbSet < Post> Posts { get ; set ; }    }
Note that you need to connect to the string and then provide the Dbset set. In future programs, you can obtain data through these sets. In main
Public static void Main (string [] args) {BlogContext ctx = new BlogContext (); ctx. blogs. add (new Blog {ID = "1", BlogName = "Jason's Blog", BlogAuthor = "Jason ", posts = new List <Post> {new Post {ID = "1", PostName = "shenwei", Content = "Today's sunshine is really good, so you have to take a nap! "}, New Post {ID =" 2 ", PostName =" shenwei ", Content =" It's raining today, so you can't go out! "}}}); Ctx. blogs. add (new Blog {ID = "2", BlogName = "Code Home", BlogAuthor = "zhangxiaomao"}); ctx. saveChanges (); Console. readKey ();
After running, the database is as follows: Primary KeyYou can find that each primary key class corresponds to a table, and the primary key automatically has a table, the reason is that EF automatically sets the ID or class name + ID field as the primary key under the EF default rule, if such a field does not exist, an error is returned. However, you can also specify the primary key by marking the [key] feature on the field in DataAnnotations. Foreign keyWhen codefirst finds that there is a one-to-many relationship between our classes, it will automatically generate foreign keys in the corresponding table, so that the relationship between classes is clear. Select * from posts select * from blogs. It's now. (Recommended !)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.