Entity Framework Code first + migrations and creating a new database - ef-code-first

I am using VS2012, framework 4.5, and Entity Framework 5.0 using code first and migrations.
I published my site using web deploy in which I changed the connection string to a new sql server 2008 db and turned on Execute Code First Migrations.
This all worked and the database tables where created but the seed method wasn't called.
Any ideas why the seed method wasn't called?

Well I found out my issue through trial and error. It looks like in order to get the code first migrations to work in the project properties you need to turn on "Include all databases configured in Package/Publish SQL tab" and then go to those settings and ensure your connection string is listed and check in the Database Entries table.
This wasn't an obvious answer to me because it looked like code first migrations were handled using web deploy settings in the publish profile so having to do it in both places doesn't seem right.

Related

After web deploy from visual studio, do I need to run dotnet ef update database to perform migration?

Sorry if the question is a bit long. I couldn't figure out how to phrase it any other way.
I recently deployed an application to google cloud via VS 2017 web deploy. And in the settings tabs there is a section where I can specify the database connection string to apply database migration.
Please let me know if I am wrong here but I though this is the destination database where the migration is applied to.
So I have set the same connection string as the database connection string for runtime and clicked on deploy.
Everything seems to be deployed without any error messages. But when I launch the application it came up with error message described in this link:
asp-net-core-deployment-to-iis-error-development-environment-should-not-be-enab
So I followed the instructions and when I browse the page, I get an error message saying that
Applying existing migrations for ApplicationDbContext may resolve this
issue
And I see that 'Apply Migrations ' blue button.
My question is , do I have to run the migrations in the command line after this web deployment normally? Or did something go wrong with preparation of the deployment, configuration or something else?
Thanks!
My question is, do I have to run the migrations in the command line after this web deployment normally?
I think this is normal.
This page describes automatic migrations:
https://msdn.microsoft.com/en-us/library/jj554735(v=vs.113).aspx
If I understand it correctly, it's actually Visual Studio that does the migration, so a deployed application will never be able to do the migration itself.

ASP.NET MVC Entity Framework application is using wrong SQL connection string

I have an ASP.NET MVC5 web application that was originally created using VS2013 and uses Entity Framework 6 Code First, with data migrations. It has been working fine (in production) for almost a year and has been re-deployed numerous times, from Visual Studio, using Web Deploy.
Today, I opened the solution in VS2015 and redeployed a known working version of the codebase to production without any problems. However, after deployment, the production website suffered from the problems described in this article, which I solved by applying author's the recommended fixes.
After applying these fixes, I now have a new problem: an exception that suggests that the application is trying to access the database using the wrong connection string:
Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. Please make sure the user has a local user profile on the computer. The connection will be closed.
Within the details of the exception page, the cause of the problem is clarified:
The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory.
I'm puzzled by this because the application has a dedicated SQL Server instance that is accessed using a regular connection string. I've checked the Web.config generated by Web Deploy and it seems to be intact. It contains a named connection string that correctly references the production SQL Server instance, and the code (which hasn't changed) correctly instantiates an Entity Framework ApplicationContext using the named connection string.
Does anyone have any idea why this application now expects to create its own SQL Server instance or what I can do to debug this problem?
Many thanks for your suggestions,
Tim
A few things you can try before making the web deploy:
Check if the project that contains the connection string declaration (web.config) is setted as the 'startup project'.
Rebuild the solution in release mode.
In the Publish Web wizard, in Settings, expand File Publishing Options and check 'Remove additional files at destination' and 'Exclude files from App_Data folder'

Running DNX (EF7) database migration on Azure

I've been running a MVC project with ASP.NET 4.6 and EF7 locally and everything works fine. I ran the dnx . ef migration Initial commands to create the database tables, and everything ran fine. The app works.
With Azure I have one problem, I can't seem to run the dnx . ef migration command so my SQL database is empty. I've debugged Startup.cs and the connection string is correctly retrieved, but the tables are not there.
I used the Publish option in Visual Studio 2015 to deploy to my Azure Web App.
How can I run this command on my web app? Is there another way to generate the database?
Thanks
It seems that the EF team has eliminated the infamous database initializers and offers a more versatile method for that on version 7.
Just use these methods in the pipeline before any call to the database,
yourDbContext.Database.EnsureCreated();
to create the database in case it doesn't exists and
yourDbContext.Database.Migrate();
to apply migrations (if you use this feature later). Both methods above also have an asynchronous version.
In my solution I have created an static class that I use to initialize the database and seed it with some data. I call the Seed method from the Configure method in the Startup class when some condition is met.

dynamic data website using ado.net entity framework and mysql giving an error just on the server

I have an asp.net dynamic data website .net 3.5 built on ado.net entity framework with mysql database.
Currently I have just one table in my entity model.
Everything is working fine on my local machine but when i uploaded on my server i got this error message:
"More than one item in the metadata collection match the identity 'mtDBModelStoreContainer'"
How can i solve this problem?
Note that this error happen when i open the table "WebsiteAdmin/users/List.aspx"
Couple of things to check:
Are there any dll's on your machine that did not get deployed.
Are there any duplicate lines in your configuration file (app/web.config)
We have performed a test and succeeded in deploying Dynamic Data Entities Web Site using dotConnect for MySQL.

Data-Tier Projects and Linked Servers

I'm trying to use a Data-Tier application involving a linked server and running into an odd problem.
Here's what I have:
1) A VS2010 solution which includes a Data-Tier project.
2) The Data-Tier project targets an SQL 2008 R2 server.
3) The SQL server has a working linked server connection to an Oracle database.
4) Views within the Data-Tier project reference the linked server using OPENQUERY
The linked server connection is valid, running queries against it on the server work fine, and creating the views directly on the server works fine as well.
However, whenever I attempt to build my Data-Tier application I get the following error:
SQL03006: View: [dbo].[vwMyExampleView] has an unresolved reference to object [MyExampleLinkedServer].
Essentially the problem is that the SQL Server Database project doesn't have a way to resolve the Link to the Oracle database's tables.
I had a similar problem when trying to reference a table in a different database on the same server. In my case I was using SQL Server for both databases, and created a database project for the other database, added it to the solution for the first project, then created a reference to the second project from the first. I also used SQLCMD variables in synonyms so that I would be able to change the synonym, and all my proc and view references would not need to be updated.
I'm not sure this will work between SQL Server and Oracle since the Visual Studio database edition only supports SQL Server. (AFAIK) You may be able to find a solution by creating a .dbschema file and adding the Database reference to your SQL Server project.
More details can be found at this link:
http://msdn.microsoft.com/en-us/library/bb386242.aspx
In SQL 2012, just add new item-> Server Objects - > LinkServer, add the sp code that creates link server, you are good to go

Resources