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.
Related
I inherited an ASP.NET MVC project with Entity Framework 5. Since I don't know whether the project was created with a code-first or database-first approach, I went ahead and added a new column to the model and then I added the new column to the database table.
I modified the codes to utilized the newly added column. Every new entry added to the database contained the data for the newly added
column. Everything seems fine running locally using Visual Studio 2017.
On the server where the project was hosted under IIS web server there is something weird happened. The data for the newly added column was not available if I published the project using Release configuration but using Dev or QA configurations the data was there.
I am not sure how to fix the problem at this point and I can use your help.
I am using EF Code first with Migrations. I need to update an old database
however I have lost some of the migration code.
Is there a way to generate the missing migrations?
Step 1: Back up ( obviously )
Step 2: Delete __MigrationHistory
Step 3: Create a temporary project.
Use the Entity Data Model Wizard to create the Code-First Model.
Step 4: Enable migrations in the new project ( using Enable-Migrations) at the Package Manager Console.
Create an initial migration ( using add-migration one). Comment out the Up and Down methods of the created migration.
Run update-database.
Step 5: Replace all the files in the Migrations folder of the original project with the files in the temporary project. Editing DbContext and namespace names as required.
Step 6 In the original project in package manager run Add-Migration two
then run update-database
In Visual Studio Community 2015 RC, I opened an ASP.NET MVC project for "individual user accounts," which uses the Microsoft.AspNet.Identity framework to create and manage users, logins, roles and permissions. It uses ASP.NET MVC 5.2 and Entity Framework version 6.
It appears that the only way to add something to this Microsoft created database is using the code first approach by first enabling migrations in the project, adding members to the DbContext inherited class and then running the update-database command at the PowerShell / Nuget package manager console. That does update the SQL Server database with the changes I made in my C# code.
However, is there a way to do the opposite? That is, to have changes that you made to the SQL Server database also reflect in your DbContext and code everywhere else? It appears that there isnt. This is a one-way street. I tried making some changes, and running the update-database command again but nothing happened.
I have an Entity Framework 6.1 'Code From Database' based project and now I need to add more tables from the database to my code.
I've spent an hour now trying to figure out how to open the Wizard to add more tables to my project.
Am I missing something or are we not able to modify a 'Code from Database' project to bring in more tables?
You can add a new item to your project: Visual C# -> Data -> ADO.NET Entity Data Model. Select Code First from Database, select the objects needed and it will generate a model class (DBSets, Fluent config, etc) and the POCOs. I've got a big database I've been bringing over in chunks that way. https://msdn.microsoft.com/en-us/library/jj200620.aspx
There is no option to edit an existing 'Code from Database' import. I ended up creating the new POCOs by adding a new data model and then deleting the newly created DbContext after moving it's DbSet<> properties into my existing DbContext.
This was quite easy to do but I was only adding a couple of tables.
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.