ef6 icon indicating copy to clipboard operation
ef6 copied to clipboard

EF6.0 scafolding don't generate NxN models anymore

Open SuperSoldado opened this issue 4 years ago • 2 comments

After updating my project from .Net 5.0 to .Net 6.0, scafolding tool missed NxN model creation when NxN table has pk over two foreing keys. Scenario: In the script below, I have table "Product" and table "ProductTag". Both tables are connected by "ProductAndTag". In EF 5.0 the model "ProductAndTag" is created when using "Scaffold-DbContext" (command below). Also in EF 5.0 "Product" model was generated like:

public partial class Product
    {
        public Product()
        {
            ProductAndTags = new HashSet<ProductAndTag>();
        }
...

Now EF 6.0 don't generate "ProductAndTag" model anymore. And "Product" model is like this:

public partial class Product
    {
        public Product()
        {
            ProductTags = new HashSet<ProductTag>();
        }

The problem is: 1)Scaffolding command is inconsistent between .Net versions 5.0 and 6.0 (another problem for our dev. team solve) 2)The proposed solution exclude "ProductAndTag" from the model. Now is impossible to include rows in this table in a direct way like this:

ProductAndTag insertObject = new (ProductAndTag);
insertObject.ProductId=100;
insertObject.ProductTagId=88;
await context.ProductAndTags.AddAsync(insertObject);

Example of scaffolding command used: Scaffold-DbContext "Server=MyConnStr" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyContext -DataAnnotations -UseDatabaseNames -force Script used in this example:

----begin script ---------------------
CREATE TABLE "Product" (
	"ProductId" int IDENTITY (1, 1) NOT NULL ,
	"Name" nvarchar (100) NOT NULL,
	"Code" nvarchar (20) NULL,
	"Description" nvarchar (200) NULL ,
	"IsActive" Bit not NULL default 0
	
	CONSTRAINT "PK_Product" PRIMARY KEY  CLUSTERED ("ProductId"))
GO

CREATE TABLE "ProductTag" (
	"ProductTagId" int IDENTITY (1, 1) NOT NULL ,
	"Name" nvarchar (20) NOT NULL,
	"Description" nvarchar (100) NOT NULL	
	CONSTRAINT "PK_ProductTag" PRIMARY KEY  CLUSTERED ("ProductTagId"))
GO

CREATE TABLE [dbo].[ProductAndTag](
	[ProductId] [int] NOT NULL,
	[ProductTagId] [int] NOT NULL,
 CONSTRAINT [PK_ProductAndTag] PRIMARY KEY NONCLUSTERED 
(
	[ProductId] ASC,
	[ProductTagId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-------------end script --------------------

EF version: Database provider: SqlServer Target framework: .Net 6.0 Operating system: Windows 10 IDE: Visual Studio 2022

SuperSoldado avatar Jun 27 '22 11:06 SuperSoldado

Breaking change doc with mitigations here

ErikEJ avatar Jun 27 '22 12:06 ErikEJ

This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.

ajcvickers avatar Jul 05 '22 09:07 ajcvickers