I have one solution (.NET framework).
In that solution I have a console application and a web application (C#).
The console application uses code first entity framework. As such, it contains the data models, their context, and all the migration folders, etc.
I am using (localDb)/MSSQLLocalDB as my server name and I can access data successfully in the console application.
The web application references the console application. Here, I have my views, controllers, and general content folders.
When I want to access data from the 'Item' table, say for instance in the 'Home' controller, I could use:
ItemContext itemTable = new ItemContext();
var firstItem = itemTable.Item.Find(0);
There appear to be no errors, the project builds successfully.
However, an exception occurs whenever I run the web application project.
System.Data.DataException: 'An exception occurred while initializing the database. See the InnerException for details.'
2 Inner Exceptions:
- EntityException: The underlying provider failed on Open.
- SqlException: Cannot attach the file 'C:SourceTrailBlazerTrailBlazer.WebApp_DataTrailBlazer.Context.ItemContext.mdf' as database 'TrailBlazer.Context.ItemContext'.
Ultimately, I am trying to avoid creating models or database related entities twice. I want to use the models in the console application.
It appears to be attempting to attach an '.mdf' file from the web application, as the database... I am not sure what that means.
What do I need to do to make this work?
When I debug the web application and step into 'itemTable.Item.Find(0);', that takes my to my console application migration Configuration.cs file and steps through the Configuration() method.
namespace SolutionName.Migrations
{
using System.Data.Entity.Migrations;
internal sealed class Configuration : DbMigrationsConfiguration<SolutionName.Context.ItemContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
}
}
Then the exception occurs immediately after returning to the controller.
Could this be something to do with my migrations configuration?
question from:
https://stackoverflow.com/questions/65648449/why-is-the-database-failing-to-initialise-in-my-net-web-application-project-wh