Why could "update database" be missing in visual studio 2012 when publishing? - c#

I'm trying to deploy an application to my webserver and everything works, except that it does not update the remote database.
With a new demo MVC project you have the option "Update database" when publishing
For some reason that options is not available when I want to publish my own MVC project.
Why is this option not showing?
I am using EF (database first)
I am getting a warning under the connectionstring fields: in order to publish a code first model you must enable EF code first migrations. (although this project should not use code first)
Web Deploy 3.0 and the dbDacFx provider are installed on the server
-- EDIT --
I have now recreated the project, and the update database keeps showing until the first web publish. then it just disappears and shows the warning in order to publish a code first model you must enable EF code first migrations. (although im not using code first, im using database first) Is this a bug in VS or what am I doing wrong?
--FIX--
I got it working by adding another connection string pointing to my database (without EF interference) in web.config: <add name="DeployCode" connectionString="Data Source=(localdb)\Projects;Initial Catalog=xxxxx;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;" providerName="System.Data.SqlClient" />

Related

Where is the DB that CodeFirst created?

Note: This is different from Cant find my EF code first database
He didn't specify the initialCatalog while I did as you will see:
I have 3 CodeFirst DBs that I work with.
1 I create and use in my visual studio project when I hit F5
1 I host in Azure with test users. If I want to update it, I simply remove it and recreate it by publishing my VS project
1 that is somewhere in my localDB, hosting it in my IIS in my machine, which is my issue. I can't find it! I don't know where CodeFirst places it. But I can access the website which somewhat brings records from it! Please help me find it.
Here is my entire setup: in my publishing profile for the Local:
lets call the blacked out "ProjAPIContext"
and in my IIS
and the www root folder is simple. The AppData folder is always empty:
and I have this in my web.config
<connectionStrings>
<add name="ProjAPIContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=ProjAPIContext; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|ProjAPIContext.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
the context matches what is in code:
internal sealed class Configuration : DbMigrationsConfiguration<ProjAPI.Models.ProjAPIContext>
That's my setup. I am looking for the DB so I can navigated through table rows and run sql queries against it, etc...
I have looked my entire harddrive with that ProjAPIContext. I see other DB created with the same times frame like tempdb and masterdb but they can't be those.
I have attach the debugger to connect to my server code in my visual studio, but nothing hit. I've run the profiler in my sql, it doesn't print the connection string. It prints useless junk.
UPDATE #1:
I also looked here:
Update#2:
of course I've looked there:
Where else should I look!?

My MVC app using EF6 started throwing an error about code generate from an EDMX file?

An in-development ASP.Net MVC app that was working fine until today has now started throwing the following error:
The context is being used in Code First mode with code that was
generated from an EDMX file for either Database First or Model First
development.
The data layer of this app was done completely in Code-first. There are not any EDMX files of any sort involved. I am using Migrations.
My connection string is
<add name="MicroContentContext"
connectionString="Data Source=MdDB;
Initial Catalog=MyCaatalog;
Integrated Security=True;MultipleActiveResultSets=True;"
providerName="System.Data.SqlClient" />
What has gone wrong and what can I do to fix it?

Server Explorer shows both dbContext and database.mdf active

I may have just confused the implementation of the database to my ASP.NET MVC project, but I have a separate data layer with a connection string pointed to a .mdf file.
I then imported the data models through the ADO.NET Entity Data Model code-first to existing database.
When I go to enable migrations, no problem.
However, now I'm trying to Add-migration, and I get:
Unable to generate an explicit migration because the following
explicit migrations are pending: [201504081848445_InitialCreate].
I checked my server Explorer, and noticed that I have both "DatabaseContext" and a Database.mdf showing up. Sorry if this is a completely elementary question, but my suspicion is that it has something to do with my connection string? I saw that when I first enable and initially create migrations, it does show up in my DatabaseContext. But the next day, I see "databaseContext" missing the _migrationHistory, and I see the error message that the explicit migrations are pending.
My connection string:
<connectionStrings>
<add name="DatabaseContext"
connectionString="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
</connectionStrings>
EDIT 1
Now i'm having trouble enabling and updating the database migrations. I've re-imported the data model code-first, and re-enabled migrations.
I see that now DatabaseContext contains only the table _MigrationHistory, and nothing else. My .mdf has all the tables except for _migrationHistory.

The model backing the '--Context' context has changed since the database was created - but db is new production database

I've got this error for the 762nd time but this time I am getting it as soon as I attempt to access my Production site, straight after deleting the 'production' database on Azure and then publishing my site.
The model backing the 'PropertyContext' context has changed since the database was created. Consider using Code First Migrations to update the database
I deleted the database because I couldn't fix this issue any other way but it still doesn't work.
Some important points:
I'm using EF6 and publishing to Azure.
This is 1 of 2 projects/sites that uses the same Repo project. I have no
problems with the other one, just this one.
I have tried publishing the problem project first (after deleting the db) and
second with the same result.
I have tried deleting both WEBSITES and the DB from Azure and starting again
I have tried deleting all migrations and starting with a fresh data model
I have tried the following in my Global.asax (in both projects)
Database.SetInitializer PropertyContext>(null); <-- SO won't let me put the first <
and
Database.SetInitializer(new MigrateDatabaseToLatestVersion<PropertyContext, MyConfiguration>());
new PropertyContext().Database.Initialize(true);
I'm using .net 4.5
Why am I getting this error on a new database and how can I get this site to work?
Just ran into the same error in ASP.Net application. In my case I did not use Code First, but I used standard ASP.Net authentication provider which apparently uses Code First, and authentication was broken because of this issue.
Here is quick and dirty solution is you don't care much about existing user records:
For me the solution was to drop the dbo.__MigrationHistory table, authentication started working fine after that. Be aware! This solution is not for everyone! This will fix the problem, but it is potentially risky.
If you cannot afford to lose data in AspNet* tables:
ASP.Net authentication provider automatically creates tables in your database:
AspNetRoles
AspNetUsers
AspNetUserRoles
AspNetUserClaims
AspNetUserLogings
The tables are empty by default, if you haven't created any new logins for your web site, you can use "quick and dirty" solution above. If you do care about preserving user information or just curios how Code First migrations work, follow these steps:
Open your Web.config file and check the name of the connection string you have for your database. It will be one of the records under <connectionStrings> element.
Open Package Manager Console:
Tools –> Library Package Manager –> Package Manager Console
In Package Manager Console window, use a drop-down to set Default Project. Make sure this is the project that contains ASP.Net authentication provider code.
Execute command:
Update-Database -ConnectionStringName MyConnectionStringName
Replace the MyConnectionStringName with the actual name you looked up in web.config.
As a result of this command you will see a new folder "Migrations" with a bunch of code generated by the Update-Database command. Re-build and re-deploy your app, your new migration code will be executed on startup and would bring the database schema in sync with an updated version of ASP.Net authentication provider code.
When using Code First with Migrations, your database creates a table called __MigrationHistory to track the current schema. When you run your application your Entity Framework will check this table to make sure that the database schema matches your database entities. If they do not match, you will get this error.
To update your database follow these steps:
Open the Package Manager Console (View -> Other Windows -> Package Manager Console) in Visual Studio
In the Package Manager Console Window, there is a drop down with your projects in, make sure it is set to the project that contains your DbContext
Make sure that the project that contains your App.Config / Web.Config file is "Set as Startup Project" (If you have multiple Configs, it must be the one with the Database Connection String defined.
Type Update-Database -ConnectionStringName MyConnString where MyConnString is the name (not the actual connection string) of your connection string in your App.Config / Web.Config
If you get an error like this: "Unable to update database to match the current model because there are pending changes and automatic migration is disabled."
You should enable Automatic Migrations and try again.
To enable Automatic Migrations
In the Migrations folder (in the project with your DbContext), open Configuration.cs.
Make sure the Constructor contains: AutomaticMigrationsEnabled = true;
To stop Entity Framework/DbContext from monitoring changes on your database you could simply delete the __MigrationHistory table in your database. It is then up to you to make sure that the database remains updated manually.
MSDN article here
The solution from this is to use the static method SetInitializer and bind to the context a Null value. If you are working on a Web solution, the best position to write the code is in the Application_Start of your Global.asax.cs file.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
//...
Database.SetInitializer<MyContext>(null);
}
I got a similar problem this morning. Suddenly the error appeared and couldn't be resolved:
The model backing the 'ApplicationDbContext' context has changed since
the database was created. Consider using Code First Migrations to update
the database
I have one project for MVC and another project for the model, context and repositories. I've been working on it for weeks but today it said stop.
I have tried to delete database, enable-migration, add-migration and update-database so many times that I've lost count. I've added initializers to MigrateDatabaseToLatestVersion as well as DropCreateDatabaseIfModelChanges. All in vain...
What finally made it work was to move model, context and repositories into the MVC project (not something I was keen on)...then it worked right out of the box without any code changes at all (besides namespaces)! Very strange...
I've read so many blog posts during the day trying to solve this problem. One of them (I don't know which one) mentioned a bug in Visual Studio 2013 where reference to DLL files weren't always updated as they should, suggesting that my MVC project missed out something when I was running add-migration and update-database in my separate project. But it's just a guess.
I'm using EF 6.1 and .Net 4.5.1 in my solution.
Got a similar problem! Answer is here
http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc3/cs/adding-a-new-field
(Rick Anderson)
There are two approaches to resolving the error:
Have the Entity Framework automatically drop and re-create the database based on the new model class schema. This approach is very convenient when doing active development on a test database, because it allows you to quickly evolve the model and database schema together. The downside, though, is that you lose existing data in the database — so you don't want to use this approach on a production database!
Explicitly modify the schema of the existing database so that it matches the model classes. The advantage of this approach is that you keep your data. You can make this change either manually or by creating a database change script.
I have spent some hours trying to solve this problem. One project was working, the other one wasn't.
I had different projects referencing different versions of Entity Framework. In my case, I had a Console app and a Windows Service app, both referencing a third project containing code first classes and DbContext.
After running Update-Package EntityFramework everything worked fine in both projects.

Add Controller database error in ASP.NET MVC 4

When using the Add Controller helper/scaffolder with Entity Framework CRUD integration, I get the following error:
Unable to retrieve metadata for 'MyApp.Models.MyModel'. Cannot attach the file
'C:\Users\Lev\AppData\Local\Temp\2d7daf19-d47f-df24-ac76-d3df4fg2sa\aspnet-MyApp-
20130409170533.mdf' as database 'aspnet-MyApp-20130409170533.mdf'.
The question is: Why is it looking for the database in my temp files? The database file is located in:
C:\Projects\MyApp\MyApp\App_Data
This is my connection string:
Data Source=(LocalDb)\v11.0;
Initial Catalog=aspnet-MyApp-20130409170533;
Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnet-MyApp-20130409170533.mdf
And so it seems that the reference to |DataDirectory| is misbehaving. How can I set DataDirectory?
If I manually put the correct path into the AttachDBFilename property, then the scaffolding works. But I'd still love to leave it as |DataDirectory|.
Just restart the Visual Studio. This helped at my project.

Categories

Resources