开发者

OnModelCreating not being called

开发者 https://www.devze.com 2023-03-15 15:31 出处:网络
I have setup one of my machines to have Sql Server 2008 R2 and have put my current database into it. I then setup a connection string in my a开发者_C百科pp.config like this:

I have setup one of my machines to have Sql Server 2008 R2 and have put my current database into it.

I then setup a connection string in my a开发者_C百科pp.config like this:

<add name="connectionString" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True;&quot;" providerName="System.Data.EntityClient" />

Then in my context constructor I have:

public MyDbContext()
    : base("connectionString")
{
}

When I do this, OnModelCreating doesn't get called.

How can I tell EntityFramework to use a specific database rather than the default .\sqlexpress?


Well for one I dont think you are correctly calling the base constructor, you would need to do this:

public MyDbContext(string connectionString) : base(connectionString)
{

}

Or if you dont want to pass in the connection string, you need to use an existing connection string variable:

static string connectionString =  // your string here

public MyDbContext() : base(connectionString) { }

What are you trying to achieve overall though, is there a need to override ObjectContext like this?

The database that EF references is whats specified in the connection string Data Source and Initial Catalog properties. The Data Source property is what determines which sql instance to reference.


The connection string wasn't quite right. Because I am creating the model using fluent (I'm guessing). This is the correct connection string:

<add name="connectionString" connectionString="localhost;Initial Catalog=MyDb;Integrated Security=True" providerName="System.Data.SqlClient" />
0

精彩评论

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