Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

c# - Why is the database failing to initialise in my .NET Web Application project, when I attempt to access data via another project in my solution?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...