EntityFrameworkCore.IndexAttribute icon indicating copy to clipboard operation
EntityFrameworkCore.IndexAttribute copied to clipboard

Table per hierarchy issue

Open radderz opened this issue 3 years ago • 4 comments

Hi,

We get an issue when we use "table per hierarchy" with this library on the PrimaryKeyAttribute.

The model is invalid as it is adding the Key attribute to the derived types. The default built in [Key] attribute doesn't cause this issue so I think something has been missed and the derived types for the Table per Hierarchy are also having the Primary Key added in the model.

	public class InventoryItemTypeCars : Inventory
	{
		public string Make { get; set; }
		public string Model { get; set; }
	}

	public class InventoryItemTypeFruit : Inventory
	{
		public string Juicyness { get; set; }
	}

	public class InventoryItem : BaseDbItem
	{
		public string InventoryType { get; set; };
	}

	public abstract class BaseDbItem
	{
		[PrimaryKey(IsClustered = false)]
		public Guid Id { get; set; } = Guid.NewGuid();
	}

....

model builder:

            modelBuilder.Entity<InventoryItem>()
               .HasDiscriminator(b => b.InventoryType)
               .HasValue<InventoryItemTypeCars>("Car")
               .HasValue<InventoryItemTypeFruit>("Fruit");


radderz avatar Apr 21 '21 05:04 radderz

This is the resulting Error: A key cannot be configured on 'InventoryItemTypeCars' because it is a derived type. The key must be configured on the root type 'InventoryItem'.

radderz avatar Apr 21 '21 05:04 radderz

Sorry too late! The notification mail of this issue was stored in my spam folder! I noticed this issue just now.

Please give me few days to investigate the problem you reported.

jsakamoto avatar Apr 29 '21 06:04 jsakamoto

Hi, @radderz

I was succeeded in reproducing this problem, and I also found a workaround for this problem.

Could you try to invoke the BuildIndexesFromAnnotationsForSqlServer() extension method at first?

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // 👇 Could you try to invoke this at first?
    modelBuilder.BuildIndexesFromAnnotationsForSqlServer();

    modelBuilder.Entity<InventoryItem>()
        .HasDiscriminator(b => b.InventoryType)
        .HasValue<InventoryItemTypeCars>("Car")
        .HasValue<InventoryItemTypeFruit>("Fruit");
}

The sample program worked fine in my development environment, and the sample program created a SQL Server database as I expected.

image

  • .NET Core SDK 5.0.202
  • EFCore 5.0.5
  • Toolbelt.EntityFrameworkCore.IndexAttribute.SqlServer 5.0.0
  • SQL Server 2019 LocalDB

See also the attached files.

Is this information helpful for you?

jsakamoto avatar May 03 '21 06:05 jsakamoto

I am having the same issue with EF Core 7 with both TPC and TPT mapping scenarios. Just as in the example presented above, the PrimaryKey attribute appears on the Id of the abstract base class, and an error is generate for every class that is derived either directly, or indirectly, from the base class.

I tried moving the BuildIndexesFromAnnotationsForSqlServer() call to the first thing in OnModelCreating() as suggested, but it did not make any difference.

jbhelm avatar Jan 19 '23 20:01 jbhelm