Skip to content

CLR property 'Number' cannot be added to entity type 'CHSIMTBase' because it is declared on the CLR type 'Contract'. #21974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Anthony-Breneliere opened this issue Aug 7, 2020 · 1 comment

Comments

@Anthony-Breneliere
Copy link

I need to create a relationship between two classes ContractItem and Contract based on a contract number.

This relationship:

  1. targets a derived class Contract
  2. does not use Contract 's principal Key named Reference but Number

When using HasPrincipalKey( c => c.Number) on the relationship Contract-ContractIem, an error tells CLR property 'Number' cannot be added to entity type 'CHSIMTBase'.

But Contract is a derived class of abstract base class CHSIMTBase.

I could not find a way to create the relationship between Contract and ContractItem.

I tried to move the property Number on CHSIMTBase but

  • the CHSIMTBase is not a Contract
  • CHSIMTBase is abstract (there is no discriminator for this type )

Steps to reproduce

Here is the model and the context to reproduce the error:


    public abstract class CHSIMTBase
    {
        public int Reference { get; set; }
        public string ContractKey { get; set; }
    }

    public class Contract : CHSIMTBase
    {
        public string Number { get; set; }
        public virtual List<CustomerContractLink> CustomerContractLinkRef { get; set; }
    }

    public class CustomerContractLink : EntityLink
    {
        public int Reference { get; set; }
        public Contract Contract { get; set; }
        // public Customer Customer { get; set; }
    }

    public class ContractItem
    {
        public int Code { get; set; }
        public int ContractNumber { get; set; }
        public Contract Contract { get; set; }
       // public Service Service { get; set; }
    }

    public class CoherisContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<CHSIMTBase>(entity =>
            {
                entity.HasKey(e => e.Reference);
                entity.ToTable("CLIENTS");

                entity
                    .HasDiscriminator(e => e.EntityCode)
                    .HasValue<Customer>( (int) EntityCodes.Customer )
                    .HasValue<Site>(     (int) EntityCodes.Site )
                    .HasValue<Partner>(  (int) EntityCodes.Partner )
                    .HasValue<Contract>( (int) EntityCodes.Contract );

                entity.Property(e => e.Reference)            .HasColumnName("REFERENCE");

            });

            modelBuilder.Entity<Contract>(entity =>
            {
                entity.HasBaseType<CHSIMTBase>();

                entity
                    .HasMany( e => e.CustomerContractLinkRef )
                    .WithOne(c => c.Contract )

                // entity
                //     .HasMany(e => e.ContractItems)
                //     .WithOne(i => i.Contract)
                //     .HasPrincipalKey(i => i.ContractKey)
                //     .HasForeignKey(i => i.ContractNumber);
                //     
                entity.Property(e => e.Number)        .HasColumnName("CL_RUB1");
            });


            modelBuilder.Entity<ContractItem>(entity =>
            {
                entity.ToTable("AF_LINK");
                entity.HasKey(e => e.Code);
                entity.Property(e => e.Code) .HasColumnName("AF_CODE");
                entity.Property(e => e.ContractNumber) .HasColumnName("AF_INFO_COMP1");

                // generate error CLR property 'Number' cannot be added to entity type 'CHSIMTBase' because it is declared on the CLR type 'Contract'.
                entity.HasOne( e => e.Contract)
                    .WithMany().HasPrincipalKey( c => c.Number).HasForeignKey( e => e.ContractNumber );
            });
        }
    }

I added the question/issue on Stackoverflow:
https://stackoverflow.com/questions/63304644/ef-core-3-clr-property-number-cannot-be-added-to-entity-type-chsimtbase-bec

Further technical details

EF Core version: 3.1.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Aug 7, 2020

Duplicate of #2611

EF Core doesn't currently support this. You can either move Number to CHSIMTBase or exclude CHSIMTBase from the model by calling modelBuilder.Ignore<CHSIMTBase>()

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants