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
I need to create a relationship between two classes ContractItem and Contract based on a contract number.
This relationship:
targets a derived class Contract
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 );
});
}
}
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>()
I need to create a relationship between two classes ContractItem and Contract based on a contract number.
This relationship:
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
Steps to reproduce
Here is the model and the context to reproduce the error:
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
The text was updated successfully, but these errors were encountered: