Why doesn't my Entity Framework code first DbContext show objects from my populated table?

I am trying to deploy my application to a test environment, but I can not get the Entity Framework to play well with the database. In development, I use a database initializer to seed the database and it works flawlessly. However, when I deploy the application to a real IIS instance, I cannot get it to interact with the database. My custom initialization rules do not execute at all, so I create the database manually.

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

My problem occurs after starting the application. I have custom membership and role providers, none of which detect that two roles exist in the database.

How to make the entity infrastructure recognize that these lines are not empty? I am currently using the private DbContext inside the repository to handle communication with the Entity Framework and have disabled my custom initializer until this hiccup is resolved.

Code that tries to find roles in the database:

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

In the Roles table, the database shows the following:

 Id Name Description 1 Company NULL 2 Customer NULL 

LINQ throws an empty sequence exception, as context.Roles is empty.

+4
source share
2 answers

The following are the steps that I took to transfer code from the first project using the special implementation of IDatabaseInitializer, which reset the database during each session to install a non-volatile database.

First, copy the schema generated by the Entity Framework in your dev. By executing the following code and placing the output in an accessible place (for example: a file on the desktop, source code in a browser, etc.). context - an instance of your class that inherits from DbContext:

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

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

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

Then delete the code that seeds the database. I used AppSetting so that I can easily switch this code usage after deployment without the need for recompilation and deployment.

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

Directly outside the scope of this statement, you still need to initialize the database, but be sure to pass false so that initialization only occurs if the database has not been initialized.

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

Finally, run your SQL installation query in an empty database to create tables with corresponding fields.

If you deploy and run the application, it should connect to the database and work as usual with any data loaded into an external script. I have successfully tested this method with custom membership and role providers.

0
source

Judging by the comments, it seems that there is no connection between the Entity Framework and the database. You do not indicate which version of the Entity Framework you are using, but I assume that you have upgraded to the latest version (4.3 at the time of writing).

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

0
source

Source: https://habr.com/ru/post/1397083/


All Articles