Thursday, June 13, 2024
HomeMicrosoft ExchangeEF Core : international key column with similar title as present navigation...

EF Core : international key column with similar title as present navigation property


 I wish to use EF Core to map a legacy database. I can not scaffold as there are a whole lot of tables and I’m solely interested by only a few and moreover scaffolding generates names for the navigation properties which aren’t those I would like. The state of affairs is: I’ve a required many-to-one relationship linked by a column title that matches the title of the property that I wish to give.

The code (simplified) is:

 

public class EntityProperty
{
    public ControlType ControlType { get; set; }
} 

public class ControlType
{
    public Checklist<EntityProperty> Properties { get; set; } = new Checklist<EntityProperty>();
}

Nothing extra is required, eg, I am not utilizing international key properties, simply navigation. 

The issue is, the international key column from the EntityProperties desk to the ControlTypes can also be known as ControlType.

So, when I attempt to map it as:

 

builder.HasOne(x => x.ControlType)
       .WithMany(x => x.Properties)
       .IsRequired()
       .HasForeignKey("ControlType");

 I wish to use EF Core to map a legacy database. I can not scaffold as there are a whole lot of tables and I’m solely interested by only a few. The state of affairs is: I’ve a required many-to-one relationship linked by a column title that matches the title of the property that I wish to give.

The code (simplified) is:

public class EntityProperty
{
    public ControlType ControlType { get; set; }
} 

public class ControlType
{
    public Checklist<EntityProperty> Properties { get; set; } = new Checklist<EntityProperty>();
}

The issue is, the international key column from the EntityProperties desk to the ControlTypes can also be known as ControlType.

So, when I attempt to map it as:

builder.HasOne(x => x.ControlType)
       .WithMany(x => x.Properties)
       .IsRequired()
       .HasForeignKey("ControlType");

I get the next exception:

 

InvalidOperationException: The property or navigation ‘ControlType’ can’t be added to the ‘EntityProperty’ sort as a result of a property or navigation with the identical title already exists on the ‘EntityProperty’ sort.’

 

The issue is with HasForeignKey, I suppose it is as a result of I’m including a shadow property when a “bodily” property already exists, however I what I actually imply to say is to make use of a unique column title to hyperlink the 2 entities: what I would like is to have the ability to specify a “not commonplace” column title for the international key, which I’m not in a position to. I wouldn’t have a international key property, solely a navigation property, and I don’t wish to have one. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments