You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In certain cases, configuring enum lookups on an owned type (via the new ConfigureOwnedEnumLookup extension method) causes EF Core to generate an enum lookup table with a redundant foreign key reference to itself. This results in a warning appearing within the Package Manager Console when creating migrations (given an enum called Month):
Microsoft.EntityFrameworkCore.Model.Validation[10614]
The foreign key {'Id'} on entity type 'EnumWithNumberLookup<Month> targets itself, it should be removed since it serves no purpose.
This is a minor issue and it's really just to bring it to your attention in case you have any idea what might be causing it. I don't believe it will affect behaviour in any way.
Steps To Reproduce
It's difficult to reproduce this issue. I've set up both a dummy .NET Core console app project and my real ASP.NET Core-based project to create the exact same minimal set of entities that exposes the issue. The relevant classes are listed below.
Domain
A "product consultant" (not modelled) can optionally have an "annual target". An "annual target" owns zero or many "monthly targets", each of which correspond to a month of the year.
publicclassAnnualTarget{protectedAnnualTarget(){// Required by EF}publicintYearStartPeriod{get;privateset;}publicintTarget=> MonthlyTargets.Sum(x => x.Target);protectedvirtualIReadOnlyCollection<MonthlyTarget> MonthlyTargets {get;privateset;}=null!;privateintProductConsultantId{get;set;}}
publicclassMonthlyTarget{protectedMonthlyTarget(){// Required by EF}publicMonthlyTarget(intyearStartPeriod,Monthmonth,inttarget){YearStartPeriod=yearStartPeriod;Month=month;Target=target;}publicMonthMonth{get;privateset;}publicintYearStartPeriod{get;privateset;}publicintTarget{get;privateset;}publicintAdjustment{get;privateset;}privateintProductConsultantId{get;set;}}
However, the generated designer scripts differ between the two projects.
Dummy .NET Console Project (working as intended)
// <auto-generated />using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Infrastructure;using Microsoft.EntityFrameworkCore.Metadata;using Microsoft.EntityFrameworkCore.Migrations;using Microsoft.EntityFrameworkCore.Storage.ValueConversion;using experiment.Data;namespace experiment.Migrations
{[DbContext(typeof(CommissionsContext))][Migration("20200917121204_InitialCreate")]partialclassInitialCreate{protectedoverridevoidBuildTargetModel(ModelBuildermodelBuilder){
#pragma warning disable 612,618
modelBuilder
.HasAnnotation("ProductVersion","3.1.8").HasAnnotation("Relational:MaxIdentifierLength",128).HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("SpatialFocus.EntityFrameworkCore.Extensions.EnumWithNumberLookup<experiment.Models.Month>",b =>{ b.Property<int>("Id").HasColumnType("int"); b.Property<string>("Name").HasColumnType("nvarchar(450)"); b.HasKey("Id"); b.HasIndex("Name").IsUnique().HasFilter("[Name] IS NOT NULL"); b.ToTable("Month"); b.HasData(new{ Id =1, Name ="January"},new{ Id =2, Name ="February"},new{ Id =3, Name ="March"},new{ Id =4, Name ="April"},new{ Id =5, Name ="May"},new{ Id =6, Name ="June"},new{ Id =7, Name ="July"},new{ Id =8, Name ="August"},new{ Id =9, Name ="September"},new{ Id =10, Name ="October"},new{ Id =11, Name ="November"},new{ Id =12, Name ="December"});});
modelBuilder.Entity("experiment.Models.Targets.AnnualTarget",b =>{ b.Property<int>("ProductConsultantId").HasColumnType("int"); b.Property<int>("YearStartPeriod").HasColumnType("int"); b.HasKey("ProductConsultantId","YearStartPeriod"); b.ToTable("AnnualTarget");});
modelBuilder.Entity("SpatialFocus.EntityFrameworkCore.Extensions.EnumWithNumberLookup<experiment.Models.Month>",b =>{ b.HasOne("SpatialFocus.EntityFrameworkCore.Extensions.EnumWithNumberLookup<experiment.Models.Month>",null).WithMany().HasForeignKey("Id").OnDelete(DeleteBehavior.Cascade).IsRequired();});
modelBuilder.Entity("experiment.Models.Targets.AnnualTarget",b =>{ b.OwnsMany("experiment.Models.Targets.MonthlyTarget","MonthlyTargets",b1 =>{ b1.Property<int>("ProductConsultantId").HasColumnType("int"); b1.Property<int>("YearStartPeriod").HasColumnType("int"); b1.Property<int>("Month").HasColumnType("int"); b1.Property<int>("Adjustment").HasColumnType("int"); b1.Property<int>("Target").HasColumnType("int"); b1.HasKey("ProductConsultantId","YearStartPeriod","Month"); b1.HasIndex("Month"); b1.ToTable("MonthlyTarget"); b1.HasOne("SpatialFocus.EntityFrameworkCore.Extensions.EnumWithNumberLookup<experiment.Models.Month>",null).WithMany().HasForeignKey("Month").OnDelete(DeleteBehavior.Cascade).IsRequired(); b1.WithOwner().HasForeignKey("ProductConsultantId","YearStartPeriod");});});
#pragma warning restore 612,618}}}
Real ASP.NET Core Project (has issue)
// <auto-generated />using MyCompany.Commissions.Infrastructure.Data;using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Infrastructure;using Microsoft.EntityFrameworkCore.Metadata;using Microsoft.EntityFrameworkCore.Migrations;using Microsoft.EntityFrameworkCore.Storage.ValueConversion;namespace MyCompany.Commissions.Infrastructure.Migrations
{[DbContext(typeof(CommissionsContext))][Migration("20200917115338_InitialCreate")]partialclassInitialCreate{protectedoverridevoidBuildTargetModel(ModelBuildermodelBuilder){
#pragma warning disable 612,618
modelBuilder
.HasAnnotation("ProductVersion","3.1.8").HasAnnotation("Relational:MaxIdentifierLength",128).HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("MyCompany.Commissions.Domain.ProductConsultants.Targets.AnnualTarget",b =>{ b.Property<int>("ProductConsultantId").HasColumnType("int"); b.Property<int>("YearStartPeriod").HasColumnType("int"); b.HasKey("ProductConsultantId","YearStartPeriod"); b.ToTable("AnnualTarget");});
modelBuilder.Entity("SpatialFocus.EntityFrameworkCore.Extensions.EnumWithNumberLookup<MyCompany.Commissions.Domain.ProductConsultants.Month>",b =>{ b.Property<int>("Id").HasColumnType("int"); b.Property<string>("Name").HasColumnType("nvarchar(450)"); b.HasKey("Id"); b.HasIndex("Name").IsUnique().HasFilter("[Name] IS NOT NULL"); b.ToTable("Month"); b.HasData(new{ Id =1, Name ="January"},new{ Id =2, Name ="February"},new{ Id =3, Name ="March"},new{ Id =4, Name ="April"},new{ Id =5, Name ="May"},new{ Id =6, Name ="June"},new{ Id =7, Name ="July"},new{ Id =8, Name ="August"},new{ Id =9, Name ="September"},new{ Id =10, Name ="October"},new{ Id =11, Name ="November"},new{ Id =12, Name ="December"});});
modelBuilder.Entity("MyCompany.Commissions.Domain.ProductConsultants.Targets.AnnualTarget",b =>{ b.OwnsMany("MyCompany.Commissions.Domain.ProductConsultants.Targets.MonthlyTarget","MonthlyTargets",b1 =>{ b1.Property<int>("ProductConsultantId").HasColumnType("int"); b1.Property<int>("YearStartPeriod").HasColumnType("int"); b1.Property<int>("Month").HasColumnType("int"); b1.Property<int>("Adjustment").HasColumnType("int"); b1.Property<int>("Target").HasColumnType("int"); b1.HasKey("ProductConsultantId","YearStartPeriod","Month"); b1.HasIndex("Month"); b1.ToTable("MonthlyTarget"); b1.HasOne("SpatialFocus.EntityFrameworkCore.Extensions.EnumWithNumberLookup<MyCompany.Commissions.Domain.ProductConsultants.Month>",null).WithMany().HasForeignKey("Month").OnDelete(DeleteBehavior.Cascade).IsRequired(); b1.WithOwner().HasForeignKey("ProductConsultantId","YearStartPeriod");});});
modelBuilder.Entity("SpatialFocus.EntityFrameworkCore.Extensions.EnumWithNumberLookup<MyCompany.Commissions.Domain.ProductConsultants.Month>",b =>{ b.HasOne("SpatialFocus.EntityFrameworkCore.Extensions.EnumWithNumberLookup<MyCompany.Commissions.Domain.ProductConsultants.Month>",null).WithMany().HasForeignKey("Id").OnDelete(DeleteBehavior.Cascade).IsRequired();});
#pragma warning restore 612,618}}}
The problem lies at the end of the designer file for the ASP.NET Core project:
In certain cases, configuring enum lookups on an owned type (via the new
ConfigureOwnedEnumLookup
extension method) causes EF Core to generate an enum lookup table with a redundant foreign key reference to itself. This results in a warning appearing within the Package Manager Console when creating migrations (given an enum calledMonth
):This is a minor issue and it's really just to bring it to your attention in case you have any idea what might be causing it. I don't believe it will affect behaviour in any way.
Steps To Reproduce
It's difficult to reproduce this issue. I've set up both a dummy .NET Core console app project and my real ASP.NET Core-based project to create the exact same minimal set of entities that exposes the issue. The relevant classes are listed below.
Domain
A "product consultant" (not modelled) can optionally have an "annual target". An "annual target" owns zero or many "monthly targets", each of which correspond to a month of the year.
Context
Outputs
The generated migration file (
..._InitialCreate.cs
) is identical between the dummy project and the real project:However, the generated designer scripts differ between the two projects.
Dummy .NET Console Project (working as intended)
Real ASP.NET Core Project (has issue)
The problem lies at the end of the designer file for the ASP.NET Core project:
The text was updated successfully, but these errors were encountered: