Cannot attach file as database in multiple projects - entity-framework

I have two projects which are targeting the same database. One of them is an MVC web application and the other is a Web API project. However, when I initialize an instance of the context in any of the projects I get the following exception:
Cannot attach the file 'myRoute/MyDatabase.mdf' as database
'MyDatabase'.
They way I have solved this is by:
Erasing my database from the SQL server object explorer (as stated in EF5: Cannot attach the file ‘{0}' as database '{1}')
Running code first migrations once again.
However, if I run the code first update while my startup project is the web application, the web services generate the same exception. If I run the code first update while my startup project is the web service, then the web application has the same issue.
What happens?

For what its worth I had this problem and apparently solved it by adding an explicit reference to the database in the MVC Web.config file EXCLUDING the AttachDbFilename clause:

Related

Windows Azure publish profile not updating database

I'm using Windows Azure to host an ASP.NET MVC4 web application. I want to use database first-programming, and followed this tutorial to create a database and data model.
I then created a website with a linked database in Windows Azure. I downloaded the publish profile and imported it through VS2012 into the project. I noticed that the database connection strings were not included, so I collected it from the database created in Azure.
In the publish wizard under tab "Settings" I was able to to check a box called "Update database" and when I published the website for the first time, everything went perfect and the website and database was uploaded.
Then I did some changes to the database, updated the data model as described in above mentioned tutorial, updated my code and built the project. This time, when I entered the Publish wizard and selected the "Settings" tab, I am no longer able to check the "Update database" checkbox. Instead there is a disabled checkbox called "Execute Code First Migrations (runs on application start)".
Why can I no longer select the "update database" checkbox? Do I have to update the database manually from now? I tried to create the datamodel again, but it did not help.
This is a completely normal behavior. You set that option for the first time and on successive deployments the code-first migrations will take place on application start. EF (Entity framework) will start the Code-First migrations then.
Especially when you update your model manually.
You will not have to update your database manually. It will be done on application start. When your web-app is started, if you select the migration option.
Read more in detail here:
EF Code First Migrations Deployment to an Azure Cloud Service

glassfish 4.0 jpa resource

I try to deploy my application but I still get deployment errors. I created connection pool succesfully (I can even create entities from tables using JPA Tools in Eclipse).
As you can see I've also created a JDBC Resource in Glassfish admin panel called sellyourthingres. When I use "sellyourthingres" instead of "sellyourthing" in persitance.xml nothing changes and i got exactly the same errors (with different name of course). Choosing JTA/JTA(default) doesn't change anything.
What should I do?

Publish Entity-Framework Code-First Migrations with no Context in the startup project

I am building a solution with the following Projects:
Main.Data - Class Library project
Main.API - Asp.NET MVC WebApi - references Main.Data
Main - Asp.NET MVC 4 web application - references Main.API
I have a MyContext : DbContext class located inside Main.Data project.
I have also successfully issues enable-migrationsconsole command on Main.Data project, and I am successfully using LocalDB as an SQL server for my data and for upgrade-database migrations.
The problem starts when I am trying to publish Main project to Windows Azure website.
The Publish Profile that is automatically created using Import from a Windows Azure web site does not seems to recognize that I am using Entity Framework Code First solution, and so I can't enable Execute Code First Migrations as I would like to. Instead, I can only enable Update database scripts.
I am using Visual Studio 2012 with Entity Framework 5.0.0 (Since the beginning of the project).
Just to verify, I have tried to add a temporary MyContext class inside the Main Project and to enable-migrations on the Main project, and after that my Publish Profile automatically detected Entity Framework Code-First.
That is, of-course, not a solution (or is it?)
Here are some relevant threads:
This is the base learning tutorial.
This explains deployment options, but no troubleshooting.
This actually gives a few ideas to try, but all seems unnatural.
I am looking for a clean stable solution. Do I have to put my Context class inside the Main Project?
Thanks in advance
I can now enable Execute Code First Migrations when I create a publish profile.
Here is what I did to achieve it:
Inside Main/Web.config I changed the name of the connection string to the FQN of the context class: Main.Data.MyContext.
Add a reference from Main project to Main.Data Project (which was not needed until now).
This does the job for me.If anyone got a better or more educating answer, I would be happy to hear it.

Error distributing WPF app that uses EF Code first

I'm developing a WPF that generates an SQLCE DB at runtime and fills it.
Everything runs fine in debug, but when I distribute an app to a 64bit Windows 7 machine I get a
Access to the database file is not allowed
error because the system cannot create the .sdf file. I think it's a permission issue but I don't know what I can do to avoid this problem, is there anything to do in setup project or a flag to give to Entity Framework for forcing DB creation?
Create the sdf file in a location where the user has write access.

Deploy Entity Framework Code First

I guess I should have thought of this before I started my project but I have successfully built and tested a mini application using the code-first approach and I am ready to deploy it to a production web server.
I have moved the folder to my staging server and everything works well. I am just curious if there is a suggested deployment strategy?
If I make a change to the application I don't want to lose all the data if the application is restarted.
Should I just generate the DB scripts from the code-first project and then move it to my server that way?
Any tips and guide links would be useful.
Thanks.
Actually database initializer is only for development. Deploying such code to production is the best way to get some troubles. Code-first currently doesn't have any approach for database evolution so you must manually build change scripts to your database after new version. The easiest approach is using Database tools in VS Studio 2010 Premium and Ultimate. If you will have a database with the old schema and a database with the new schema and VS will prepare change script for you.
Here are the steps I follow.
Comment out any Initialization strategy I'm using.
Generate the database scripts for schema + data for all the tables EXCEPT the EdmMetadata table and run them on the web server. (Of course, if it's a production server, BE CAREFUL about this step. In my case, during development, the data in production and development are identical.)
Commit my solution to subversion which then triggers TeamCity to build, test, and deploy to the web server (of course, you will have your own method for this step, but somehow deploy the website to the web server).
You're all done!
The Initializer and the EdmMetadata tables are needed for development only.