About DbContext in the EF Entity Framework

Source: Internet
Author: User
Tags connectionstrings

EF4.1 includes code first and DbContext APIs. The DbContext API provides more ways for EF to work: Code First,database First and model first.

Using the DbContext constructor

1. Code First Contract connection

Namespace Magic.unicorn
{
public class Unicornscontext:dbcontext
{
Public Unicornscontext ()
C # would call base class parameterless constructor by default
{
}
}
}

The connection string (SQL Express) for the database is generated on this computer using Magic.Unicorn.UnicornsContext as the database name.

2. Code first specifies the contract connection for the database name

public class Unicornscontext:dbcontext
{
Public Unicornscontext ()
: Base ("Unicornsdatabase")
{
}
}

The connection string (SQL Express) for the database is generated on this computer using Unicornsdatabase as the database name.

3. Code first uses the connection string in the configuration file

<connectionStrings>
<add name= "Unicornscedatabase"
Providername= "system.data.sqlserverce.4.0"
connectionstring= "Data source=unicorns.sdf"/>
</connectionStrings>

public class Unicornscontext:dbcontext
{
Public Unicornscontext ()
: Base ("Name=unicornscedatabase")
{
}
}

4. Database/model connection characters in the config file for first use

<add name= "Northwind_entities"
Connectionstring= "
metadata=res://*/northwind.csdl|
res://*/northwind.ssdl|
RES://*/NORTHWIND.MSL;
Provider=system.data.sqlclient;
Provider Connection string=
&quot;data source=.\sqlexpress;
Initial Catalog=northwind;
Integrated security=true;
multipleactiveresultsets=true&quot; "
Providername= "System.Data.EntityClient"/>

public class Northwindcontext:dbcontext
{
Public Northwindcontext ()
: Base ("Name=northwind_entities")
{
}
}

About DbContext in the EF Entity Framework

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.