EntityFramework start a small trial

Source: Internet
Author: User
Tags connectionstrings

1 Install-package entityframework

2 Creating an Entity class

public class blog{Public    int BlogId {get; set;}    public string Name {get; set;}}

3 Creating a context

public class Eftestcontext:dbcontext
{
Public Eftestcontext (): Base ("Name=sqlmonitor") {}


Public dbset<cgroup> CGroups {get; set;}
Public dbset<address> Addresses {get; set;}
Public dbset<contact> Contacts {get; set;}
protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
Base. Onmodelcreating (ModelBuilder);
Modelbuilder.entity<contact> (). Hasoptional (c = c.address)
. Withoptionaldependent (add = Add.) Contact);
Modelbuilder.entity<cgroup> (). Hasmany (c = c.contacts)
. Withrequired (c = c.cgroup). Willcascadeondelete (FALSE);
Modelbuilder.entity<easyuiefwebapp.dal.efmodels.contact> (). Hasoptional (c = c.cgroup)
// . Withmany (c = c.contacts). Willcascadeondelete (TRUE);
}
}

4 Set the Data Link link note to match the name of the context, otherwise create the local database

<connectionStrings>    <add name= "SQLMonitor" connectionstring= "Data source= (LocalDb) \v11.0;initial catalog=sqlmonitor;integrated security=sspi; "providername=" System.Data.SqlClient "/>  </ Connectionstrings>
Enable-migrations
Execute the above command in NuGet, if the error, 1 please see whether to write errors, 2 is the EF project, 3 after compiling try 4 to see if the version of NuGet is too old
6 Update-database

Update to the database. Note that some of the article is written add-migrations initialcreate I executed the error, no solution,
With Update-database, there is no error, and the database is updated correctly.
7 add-migration Note the following names, such as add-migration modifyusertable
Similar version-controlled stuff

EntityFramework start a small trial

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.