Page 1 of 1

Reference to a table that is TPT (always update)

Posted: Mon 09 May 2022 13:43
by ruda
Version: 6.12.1419

I detected a problem, to occur was like this:

Code: Select all

CREATE DATABASE TestED
GO

USE [TestED]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Animal](
	[AnimalId] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Animal] PRIMARY KEY CLUSTERED 
(
	[AnimalId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Cat](
	[AnimalId] [int] NOT NULL,
	[FieldB] [int] NOT NULL,
 CONSTRAINT [PK_Cat] PRIMARY KEY CLUSTERED 
(
	[AnimalId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Dog](
	[AnimalId] [INT] NOT NULL,
	[Field1] [NVARCHAR](10) NOT NULL,
 CONSTRAINT [PK_Dog] PRIMARY KEY CLUSTERED 
(
	[AnimalId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[TestRef](
	[TestRefId] [INT] IDENTITY(1,1) NOT NULL,
	[TesAB] [NVARCHAR](50) NOT NULL,
	[AnimalId_Dog] [INT] NOT NULL,
 CONSTRAINT [PK_TestRef] PRIMARY KEY CLUSTERED 
(
	[TestRefId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Cat]  WITH CHECK ADD  CONSTRAINT [FK_Cat_Animal] FOREIGN KEY([AnimalId])
REFERENCES [dbo].[Animal] ([AnimalId])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Cat] CHECK CONSTRAINT [FK_Cat_Animal]
GO

ALTER TABLE [dbo].[Dog]  WITH CHECK ADD  CONSTRAINT [FK_Dog_Animal] FOREIGN KEY([AnimalId])
REFERENCES [dbo].[Animal] ([AnimalId])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Dog] CHECK CONSTRAINT [FK_Dog_Animal]
GO

ALTER TABLE [dbo].[TestRef]  WITH CHECK ADD  CONSTRAINT [FK_TestRef_Dog] FOREIGN KEY([AnimalId_Dog])
REFERENCES [dbo].[Dog] ([AnimalId])
GO

ALTER TABLE [dbo].[TestRef] CHECK CONSTRAINT [FK_TestRef_Dog]
GO
I add the first 3 tables for me to create the TPT
Looking like this:

Image Image Image Image

I find a bug here as I create the TPT and then remove the references to the Animal table it doesn't update the diagram, because the AnimalId fields are still visible. But if I save and then reopen the model it disappears.
But something that does not impact. But I spent a few hours thinking I was doing something wrong.

Then I add another TableRef table to the model.

With that it does not create the reference to the Dog table.
And it stays in the update loop.

Image Image

If I add all the tables before creating the TPT it adds the reference correctly.
But it stays in the update loop too.

Image

Re: Reference to a table that is TPT (always update)

Posted: Wed 11 May 2022 19:13
by DmitryGm
Please clarify:

1.) The type of the Model you create

2.) How did you remove the references? Remove from the database? From the diagram in the Entity Developer?

3.) How did you update the diagram after removing references?

Describe your actions in detail and step by step.

Re: Reference to a table that is TPT (always update)

Posted: Wed 11 May 2022 21:23
by ruda
1. EF Core
2. It is not removed. the Entity Developer that does not create.
3. Yes

Re: Reference to a table that is TPT (always update)

Posted: Thu 12 May 2022 15:09
by DmitryGm
ruda wrote: Wed 11 May 2022 21:23 1. EF Core
2. It is not removed. the Entity Developer that does not create.
That is EF Core Model I Created:

Image

As I can see, the references were created.

Re: Reference to a table that is TPT (always update)

Posted: Thu 12 May 2022 15:13
by ruda
The issue at hand is the TestRef table reference with Dog.
The TPT is created correctly.

Re: Reference to a table that is TPT (always update)

Posted: Fri 13 May 2022 11:18
by DmitryGm
To add the new references you should update model from database with the option "recreate model".

Image
Image
Image
Image
Image
Image
Image

Re: Reference to a table that is TPT (always update)

Posted: Fri 13 May 2022 11:47
by ruda
The solution presented does not make sense.
It's creating a new model.
I lose all the renamed properties, all the created TPTs…
A production project with more than 500 tables is unfeasible.

The problem remains.
Rebuild model is not intended to resolve update issues.

Re: Reference to a table that is TPT (always update)

Posted: Mon 16 May 2022 07:09
by DmitryGm
We have reproduced the issue of updating EF Core Model and the investigation is in progress. We shall inform you as soon as we have any results.