Question

I am attempting to deploy my app to a testing environment, but cannot get Entity Framework to play nicely with the database. In development, I use a database initializer to seed the database and that has worked flawlessly. However, when I deploy the application to an actual IIS instance I cannot get it to interface with the database. My custom initialization rules aren't run at all, so I am instead creating the database manually.

I used the ObjectContext.CreateDatabaseScript() as a starting point for my SQL script, and SSMS verifies that two rows are populated in the appropriate table.

My problem arises after running the application. I have custom membership and role providers, neither of which seem to detect that the two roles exist in the database.

How do I get my entity framework to recognize that these rows aren't empty? I'm currently using a private DbContext inside a repository to handle communication with Entity Framework, and disabled my custom initializer until this hiccup is resolved.

Code that attempts to find roles in the database:

context.Roles.Single(r => r.Name == role)

The database shows the following in the Roles table:

Id  Name     Description
1   Company  NULL
2   Customer NULL

The LINQ generates an empty sequence exception, as context.Roles is empty.

Was it helpful?

Solution 2

Here are the steps I went through to migrate from a code first project with a custom IDatabaseInitializer implementation that reset the database during each session to a non-volatile DB setup.

First, copy the schema generated by Entity Framework in your dev. project by running the following code and placing the output in an accessible location (ie: file on desktop, raw text in browser, etc). context is an instance of your class that inherits from DbContext:

((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript()

Second, save the string returned from that call in a SQL file. Be sure to remove the command that creates the Entity Framework meta data table. This command should resemble the following:

create table [dbo].[EdmMetadata] 
( 
    [Id] [int] not null identity, 
    [ModelHash] [nvarchar](max) null, 
    primary key ([Id]) 
);

Next, remove the code that seeds the database. I used an AppSetting so that I can easily toggle that code's usage after deployment without needing to re-compile and deploy.

if (ConfigurationManager.AppSettings["Mode"] == "Dev")
{
    Database.SetInitializer<PonosContext>(new PonosInitializer());
    new MyContext().Database.Initialize(true);
}

Immediately outside of that statement, you still need to intialize the database, but make sure to pass in false so that the initializing will only happen if the database has not been initialized.

new MyContext().Database.Initialize(false);

Finally, run your setup SQL on an empty database to have the tables created with appropriate fields.

If you deploy and then run your application, it should connect to the database and be able to work as normal with any data you loaded in an external script. I tested this method successfully with custom membership and role providers.

OTHER TIPS

Judging by the comments it looks like there is no connection between Entity Framework and the database. You don't mention which version of Entity Framework you are using but I assume you have updated to the latest (4.3 as of time of writing).

I notice you say that you are "creating the database manually.". Why not open a test project and create a new database first model based on your manually created database? This should at least confirm that you can use Entity Framework with your configuration. From there I would create another project from scratch to test the Code First approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top