I'm wondering if this is even possible. I'm writing an app in MVC using ASP.NET Core and EF Core. For the most part I've been doing code-first migrations (that's all I know how to do, as yet) for my entities.
Someone added a table to the database I'm using, and rather than deleting their table and doing it the code-first way, I'd like to just bring their table over using a DB first migration.
Is that even possible to mix and match DB-first/code-first techniques?
If it is, how do I do it? I can't seem to find anything about bringing over just one table. Only migrating a whole database, which is not what I want.
Yes, it's possible. The steps are the following:
Right click on your Models folder.
Select Add -> Add new Item
In the Add New Item Window, select Data -> ADO.NET Entity Data Model. (This would create a new DbContext, but we going to merge the two DbContext) Click Add
In the Entity Data Model Wizard select Code First from database.
Select your connection String.
Click in no, exclude sensitive data...
Uncheck Save connection settings.
Click next
Select the new Table
Click finish
In this point VS would generate two files: a new DbContext and the model for the table.
Now open de new DbContext cut the DbSet of your model and paste in your original DBContext, too cut the content of the OnModelCreating method and paste at the end of the OnModelCreating method of your Original DbContext.
The final step is add a new Migration ignoring the changes.
For example:
Add-migration NewTableAdded -IgnoreChanges -verbose
Related
My ASP.NET MVC 4 application uses Entity Framework 6 and a database-first approach. It is already in production environment, and now wants to add new column to the Deposit table and this column is not null. The production table already has a lot of data.
How should I manage this changes, in code-first approach, can anyone help me out? What should I do, I am new to Entity Framework.
If you will continue to use the database-first approach you can do something like that:
1) run a script to migrate DB:
ALTER TABLE Deposit ADD <column_name> INT NULL
GO
UPDATE Deposit SET <a valid not null values for your column>
GO
ALTER TABLE Deposit ALTER COLUMN <column_name> INT NOT NULL
GO
2) Since you are using the db-first approach you need update edmx model (.edmx file) which is already attached to db. If I remember correctly, you can do it from context menu in Solution Explorer.
I am working on a project in which I am assigned to implement database first Approach. Here, I want to know that when we initiate database first approach we map that to an existing DB, but what if I have another DB with the same structure but different data, can I use that DB by just changing the connection string ? or will it impact somehow?
It will work when you change the connection string. I recommend you select the 'Code First from database' when creating new 'ADO.NET Entity Data Model' with VS add new item.
I have a Database , and I am proceeding with EF DB first approach .
Right click on Model folder
--> select new item
-->ADO.net Entity Data model.
then selecting the DB & finally clicking Finish.
After that I am not getting the Model Diagram of my DB. Not getting any Diagram for any of the tables.
The Default view is ADO.net Entity Data model Designer
Where is the issue? EF version is latest Stable(6.1.3).
enter image description here
Did you select the tables?
https://magnusmontin.files.wordpress.com/2013/05/wizard2.png?w=590&h=515
Or remove ef and then later add your project
Your answer is that your current version of the asp.net web application form's entity framework does not match the entity framework version while you create your DB first model
I have an MVC web application with code-first Entity Framework. We install this application in various computers as a local application. I made a migration to upgrade the database (in this case I added a new table), and after running the migration on upgrade, I want to insert initial data to the database so the users will be able to add/edit/delete them but I don't want the table to be empty at the first time.
Is there a way to do it automatically on upgrade without running a SQL script manually?
Migration class has up method,you can override it and insert/update records using SQL :
public override void Up() {
AddColumn("dbo.Posts", "Abstract", c => c.String());
Sql("UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL");
}
(Source)
Yes there is. You essentially write a class to conditionally check and insert values, and then you link this class to your entity framework database initialiser. It runs each time there is a migration to be performed, but I think you can change exactly when it runs (e.g. Application startup).
This link will give you the rough idea:
Entity Framework Inserting Initial Data On Rebuild
I have an exact code sample on my PC but I won't be on it until tomorrow. If this link doesn't quite do what you want, I can send you some code tomorrow which definitely will.
So I have a database and have created a model from it. Everything works just fine. But now I want to add more tables to my database and produce models from them.
Is that possible with EF Database first? Do I have to recreate all models for it to add new ones? All tables are of course from same database.
Example:
Let's say I have Table1, Table2, and Table3.
And by using DBContextGenerator in VS I've generated models from .edmx file. So I'd have Model1, Model2, and Model3 for corresponding database tables, and I'd like to add Table4 and Model4.
Do I have to recreate everything or is it possible to add somehow?
Open up your .edmx in design view.
Right click in there somewhere and choose "Update Model from Database".
On the "Add" tab, expand "Tables", and find your new database tables.
Check them, and click finish.
I've done this regularly as the DB changes and tables are added during development, it's been very reliable.
To generate the classes, do the following:
Right click on the .tt file of the solution explorer.
Click on the "Run custom tool".
Click on "OK" for the warning.
new classes will be generated.