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