1

I have two ASP.NET MVC5 projects, I want to connect them to the same database. I created a class library that contains the models and the DbContext.

The first app connects correctly, but when I run the second one I get an error says :

Cannot attach the file 'c:\users\saibi\documents\visual studio 2015\Projects\MonPFE\MonPFE.Frontend\App_Data\MonPFEContext-20161121115318.mdf' as database 'MonPFEContext-20161121115318'

PS: I have web.config in each project.

1
  • 1
    It seem that latter project is using local database instead of the database the former one using. I believe you want to update database connection strings point to same database. Commented Nov 23, 2016 at 19:29

1 Answer 1

2

Make sure you have the same connection string in each Web.config and that they point to the same database. Beware that relative paths for AttachDbFilename will point at different directories.

I'm betting you have Connection Strings that look something like this:

<connectionStrings>
    <add name="MonPFEContext" connectionString=".\SQLExpress;AttachDbFilename=|DataDirectory|MonPFEContext.mdf; Database=MonPFEContext;Trusted_Connection=Yes;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

|DataDirectory| will typically be the App_Data folder in each web project. The database file exists in one web application, but not the other. Update AttachDbFilename to point to a full file path, or use a relative path to the same file.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't get attention to the database path, Thanks @akatakritos it works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.