Entity Framework 6.1-database First Introduction

Source: Internet
Author: User

Original: Entity Framework 6.1-database First Introduction

This approach is a more traditional database-centric development model. It is more appropriate for a team with database DBAs, or for a database that already exists.

Advantages and Disadvantages

1, the advantage: the least way to edit the code, in the premise of a complete database, you can almost without editing any code can complete the application of the data layer (EF).

2, not flexible enough, the domain model structure is completely generated by the database control, the structure is not necessarily reasonable; affected by database tables and field names, the naming is not canonical.


To create a step: 1. Create a new Dal folder in the program (or separate the DAL project),


Right-click the 2.DAL folder and select Add-new item. Open the new item interface and select the data->ado in the interface. NET Entity Data model. The data model is renamed to TESTEF.EDMX, which opens the Entity Model Creation wizard.



3. Select Generate From Database


4. Select a database connection (no new one can be created), click Next



5. You can select all the tables, views, and stored procedures and functions.


Optionally determines the single plural form of the generated object name, if the database table is negative, such as books, the corresponding object name created is book, if not selected, the corresponding object name is books.

6. Click Finish to generate the EF data model



7. Test code

static void Main (string[] args)
{
Database.setinitializer (New dropcreatedatabaseifmodelchanges<entities> ());
Contact con = new Contact
{
Name = "HHT"
};
Contact Con2 = new Contact
{
Name = "Zhansan"
};
var list = new list<contact> ();
List. Add (con);
List. ADD (Con2);
CGroup Group = new CGroup
{
Contacts = list,
Gname = "Master Group"
};
using (entities db = new entities ())
{
Db. Cgroups.add (group);
Db. SaveChanges ();
foreach (CGroup item in DB. CGroups)
{
var cons = Item. Contacts;
Console.WriteLine ("Group:" + item.) Gname + "has contact" + cons. Count () + "one");
Db. Cgroups.remove (item);
}
Db. SaveChanges ();
}
}

Output results


Database



OK, insert data successfully


Entity Framework 6.1-database First Introduction

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.