Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core EF 2.1 Scaffolding - DatabaseFirst - How to add more tables

I have a clean project solution (intended to be a .NET Core MVC app)

I have two projects - Home.CorePoC.EntityModel and Home.CorePoC.Model I am calling dotnet cli from Home.CorePoC.Model.

The idea is that I want partial classes and logical separation from custom attributes and attributes from database.

I want to write a PowerShell script that I will be calling when I want to add/remove some tables, columns in tables etc.

In dotnet CLI when I add initially tables, it works just fine:

Scaffold-DbContext "Server=sqlServ2016;Database=TestDb;Persist Security 
Info=False;User ID=admin;Password=admin;" Microsoft.EntityFrameworkCore.SqlServer 
-Tables "Table1","Table2","Table3" -ContextDir "..\Home.CorePoC.EntityModel" 
-OutputDir "TestDB" -force -DataAnnotations -UseDatabaseNames

Now, when I want to add just more tables it work as well but I have an error message: "Cannot add 'Table4'. There is already a linked file in this folder with the same name." Also, I get error in cli console:

Exception calling "AddFromFile" with "1" argument(s): "A drag operation is already in progress (Exception from HRESULT: 0x80040103)"

Must I delete all files and then recreate it (linked file TestContext.cs that was initially created) or there is different way how to avoid this error message which is non-critical (I successfully added Table4 to my models).

I am planning to put this statement into PowerShell script which it will be called from project folder (where I have installed nuget Microsoft.EntityFrameworkCore.Tools(2.1.4))

like image 837
lonelydev101 Avatar asked Oct 26 '25 09:10

lonelydev101


1 Answers

That is because it will try and add the Context file for Table4 but is unable to as one already exists. There is already a feature request to update Entity Framework Core scaffolds rather than having to recreate them every time. You would have to override the generated entities each time by using the -Force flag

like image 107
Tubs Avatar answered Oct 29 '25 00:10

Tubs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!