开发者

Change EF 4.1 Code First Default DB Location

I'm working through the Building an MVC 3 App with Code First and Entity Framework 4.1 tutorial on MSDN and got stuck on "Also by default, this database will be a SQL Express database with the name derived from th开发者_C百科e strongly typed name of the context and its file will be in the SQL Express default data folder."

If I want to change the default (e.g. to place the MDF file in my App_Data folder) how would I do that? I will have several different contexts (one for each major functional area) and would like them all to live in the same database.


You define where the database lives using the web.config file connection settings. You just have to make the Context Name = your connection string name so if the of you Context is MyContext you could define the location as below:

    <connectionStrings>
    <clear/>
        <add name="MyContext" 
         connectionString="Server=myServer;Database=MyDB;Uid=foo;Password=XXX; " 
         providerName="System.Data.SqlClient"
           />


  </connectionStrings>


In answer to your second question...

"I will have several different contexts (one for each major functional area) and would like them all to live in the same database."

Why not try...

MyContext.cs

public partial class MyContext : DbContext
{
}

FooContext.cs

partial class MyContext
{
    public DbSet<Foo> Foos { get; set; }
}

BarContext.cs

partial class MyContext
{
    public DbSet<Bar> Bars { get; set; }
}

You'll end up with one context and one connection, but your code is split over multiple files. Hopefully that's what you are trying to achieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜