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

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.

Related

ASP .Net Identity and Entity Framework Database

I'm developing an event page using entity framework and asp net identity 2.0 and I'm new also with this kind of tools.
If I create a new project that using template visual studio, it's automatically create a DB with several table. and also there are many code that I don't need in that template. (template -> project mvc with already installed authentication).
I've read several tutorial, but for creating from scratch my step is like this :
Create a DB
Create simple mvc project and install package nuget for asp net identity and EF framework
Set the connection strings
Create the code and EF will automatically created the table if not exist on DB?
I'm not really sure with number 4.. and are my steps are correct?
Thanks
Just follow the tutorial step by step, this will teach you how to create and build your project in a very simple way.
Getting started with ASP.NET Identity:
https://www.captechconsulting.com/blogs/Customizing-the-ASPNET-Identity-Data-Model-with-the-Entity-Framework-Fluent-API--Part-1
and here is a good Introduction to ASP.NET Identity:
http://tektutorialshub.com/asp-net-identity-tutorial-basics/

Cannot attach file as database in multiple projects

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:

Does DotNetOpenAuth 4.3 work with Entity Framework 6?

I created an empty project in VS2013 and added these packages:
DotNetOpenAuth.AspNet
Microsoft.AspNet.Providers.Core
Microsoft.AspNet.Providers.LocalDb
Microsoft.AspNet.Membership.OpenAuth
In the process, Entity Framework 5 was added to the project. I manually installed EF6 and now I get this error when I try to authenticate a user with an external provider:
"Method not found: 'System.Data.Objects.ObjectContext System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'."
I checked the DNOA documentation but it does not say a word about dependencies/requisites.
Does anybody know if DotNetOpenAuth can work with EF6?
DotNetOpenAuth does not have EntityFramework as a dependency. The samples however do use EF. You must have some of your own code that uses entity framework.
That said, it sounds to me like you need to add a binding redirect or jiggle your entityframework configuration a bit in your web.config file.

How to migrate Entity Framework DB on publish via MsDeploy

For my current project i would prefer to update my database to the correct version on publish instead of first run via the App_Start.
It seems that there is support for manual upgrades via a commandline tool 'migrate.exe' which is supplied together with Entity Framework (5.0 rc2). But i can not find any information about how other people have combined this tool together with msdeploy to upgrade the database on publish instead of first run.
I have found the following page which explains how to use the migrate.exe tool:
http://blog.overridethis.com/blog/post/2012/03/13/Automating-EF-43x-Data-Migrations-in-your-Build.aspx
But this forces me to somehow 'know' the connectionstring information.
My optimal solution would be something like:
connectionstring is configured in setParameters.xml file for
replacement on publish
on publish after publish of codebase
migrate.exe gets called with correct connectionstring
It seems that what i search for is an 'entity framework MSDeploy provider' (see: http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-released.aspx) but it seems that they abandoned that option some time ago
You have two options:
Configure Visual Studio Databases option in the Publish Profile; or
Configure your project properties on Package/Publish SQL Tab.
I didn't use any of them yet but I was researching because I'll have the exactly same scenario.

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.