I am developing an application in ASP.NET MVC 5 and Entity Framework with a code-first approach.
Firstly, I had one class in my data context file and everything worked. enable-migrations, add-migration and update-database were working fine but second time, I added more classes and tried to execute these statements:
enable-migrations -ContextTypeName IdentityDb -MigrationsDirectory DAL\IdentityMigrations
enable-migrations -ContextTypeName SMSContext -MigrationsDirectory DAL\SMSMigrations
add-migration -ConfigurationTypeName SMSApp.DAL.IdentityMigrations.Configuration "InitialCreate"
I get an exception:
A previous migration called 'InitialCreate' was already applied to the target database. If you meant to re-scaffold 'InitialCreate', revert it by running 'Update-Database -TargetMigration $InitialDatabase', then delete '201409261933262_InitialCreate1.cs' and run 'Add-Migration InitialCreate' again.
In Solution Explorer click icon Show All Files. Than go to App_Data, right click on mdf file and open.
In Server Explorer click on connections and right click on your connection then close it, then right click again then delete it, then delete the mdf file from the Solution explorer(this will move it to the trash bin, it is not lost yet if you decide to get it back). Then in power shell package console update-database.
NOTICE: This will totally rebuild your database inserting records from Configuration/Migrations.cs Seed method, you will lose other data. Also find AutomaticMigrations in the code and set it to true.
Related
I am trying to load entites to my asp.net web api using DataBaseFirst Approach .
After I run this command in my Package Manager:
Scaffold-DbContext "Server=myserver;Database=myDatabase;UID=user;PWD=pass;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
I get the Models Folder with entities and the DbContext, but in the process I get many errors of this type:
tf14092 : impossible to modify the element x. a parent of this item has a pending delete which must be checked in first
What does this error means? How to fix it?
According to your screenshot, there is a x symbol with wwwroot folder.
First check to see if you have pending deletes. Kind like below in pending change and source control explorer
If there are , choose to check in or undo pending change/delete. Then check again.
If there are not any pending delete. Try to clear TFS cache and restart Visual Studio. Which may do the trick.
I am creating my first ASP.NET MVC application and I have a problem with Entity Framework.
I created 4 tables, added them to DbContext object and then I saved my project and closed it. All of these tables are in server explorer -> tables.
Then I created next 5 tables and added them to DbContext object. Unfortunately I cannot find them in server explorer -> tables. I tried to refresh, delete tables and add one more time but it does not work.
Do you know how to solve this problem (I used code first method) ?
Yes, you are missing migrations. Migrations are used when you need to update your database schema. If you add those tables (models) to your context it wont create them in your database until you add the migration.
With migration, it will automatically update the database schema, when your model changes without losing any existing data or other database objects.
Source
1. step
Run Visual Studio -> Tools –> Library Package Manager –> Package Manager Console
2. step
Run the Enable-Migrations command in Package Manager Console - Enables migrations on your project that contains your context.
3. step
Add-Migration AddedFiveNewTables - Prepares the script that contains all the new changes to your database.
4. step
Update-Database - Runs the previously added migration and updates your database.
I am working with ASP.NET C# MVC 5.
.mdf/.ldf files in App_Data do not appear on Pending Changes in team explorer. Therefore I can't check them into TFS(visual studio online). I've tried recreating the project a few times and none worked. What might be the cause for this?
Go to Team Explorer and locate Excluded Changes and you should find it excluded. Right click the App_Data folder and include it.
But you might want to reconsider including the database files. As you're developing and testing, every little database interaction will trigger a change and most of those are trivial. Also, if someone else is working on this project, they may not want your database file to overwrite theirs when they Get Latest Version.
If you're using Entity Framework Code-First, the database is automatically generated when you build the project I believe, otherwise you just run the Update-Database command to do it. This lets everyone collaborating have their own local database file to work with. You can also utilize migrations to make updates to the database structure. If you want the database to be generated with pre-populated data, you should utilize the Seed method.
This is a general question. As already answered by 'Ty Morrow' in above comment there is an initial Seed Method in Entity Framework which ensures that all values are inserted. However there are many scenarios that you also need to work with the latest added / removed DB entries not present in the seed method.
Please perform the following steps to ensure that your data directory file is included in the source control.
Click App_Data folder and on encircled toolbar click on Show All Files as shown below in the snapshot
Right Click on your MDF (Data Source) File and click on Include in Project
Right Click again on your MDF File and click on Include in Source Control
Simply Check in the file by Right Clicking on root project folder link and file show be now part of Source Control
I've a project following the course of John Papa about SPA.
So, my architecture is as follows:
**proj.Data.Data**
/Configuration
Mappings of the entities with Fluent
ProjContext.cs
ProjInitializer.cs
EFRepository.cs
/Migrations folder
**proj.Data.Model**
Pocos (Entity Framework 5 Code First)
No reference to Entity Framework
**proj.Data.Contracts**
Interface for IRepository and IProjContext
**proj.UI.Web**
MVC 4/WebApi project
I had an existing database and I ran reverse engineering of Entity Framework to get the entities and separated the context from the entities (in two different projects as shown above)
Then I ran (selecting proj.Data.Data from the PackageManager Console drop down):
Enable-Migrations -force -StartupProjectName "Proj.Data.Data"
add-migration InitialSchema
Update-Database
Update-Database -Script -SourceMigration $InitialDatabase
I only see in the folder Migrations in the proj.Data.Data project a file named 201211091944388_InitialSchema with the up and down methods empty. No MigrationHistory table created.
What do I need to run to get the sql script to generate the database from scratch with all the tables I have got from the entities?
Thanks! Guillermo.
NOTE: I have .Data projects in a solution folder named Data and Web project in a solution folder named UI, Should I take any extra step because of that?
I don't know why it didn't work with the reverse engineering, but I made the model again following all the conventions and it worked.
BTW, if we run the enable-migrations command on an existing database it will not create __MigrationHistory table nor the Initial script. This is as design to avoid changes in the existing database.
Thanks anyways.
I am using Entity Framework's code first approach and updating my database with changes to my DB model using the following command.
Update-Database -Script
This was all working very well until recently I added some connection strings in the App.Config for my testing application. Now when I run the update-database -script command I get the following error. Does anyone know why this is happening or how to suppress it? Thanks in advance.
Update-Database : The project 'MyProject.Tests' does not contain any migration contexts.
Check on you Packet Manager console that the Source Project is the same where the Migrations Folder exists.