r3plica
r3plica

Reputation: 13367

.net core entity framework both relationships could use the same foreign key

I have a few models that inherit from a base model:

public class Filter
{
    public int Id { get; set; }
    public int FeedId { get; set; }
    public FilterOperator FilterOperator { get; set; }
    [Required, MaxLength(100)] public string Name { get; set; }
    [Required, MaxLength(100)] public string Expression { get; set; }
    [Required, MaxLength(100)] public string FieldName { get; set; }
}

and then I have this other model:

public class Conversion : Filter
{
    public MathOperator MathOperator { get; set; }
    public double Value { get; set; }
}

These all have a similar relationship to an entity called Feed:

public class Feed
{
    public int Id { get; set; }
    [Required, MaxLength(100)] public string CategoryId { get; set; }
    [Required, MaxLength(2083)] public string Url { get; set; }
    public FeedType Type { get; set; }
    public bool Active { get; set; }

    public Category Category { get; set; }
    public ICollection<Conversion> Conversions { get; set; }
    public ICollection<FieldMap> FieldMaps { get; set; }
    public ICollection<Filter> Filters { get; set; }
    public ICollection<Rule> Rules { get; set; }
    public ICollection<Transformation> Transformations { get; set; }
}

When I try to run my add-migration command I get this error:

Both relationships between 'Conversion' and 'Feed.Conversions' and between 'Filter' and 'Feed.Filters' could use {'FeedId'} as the foreign key. To resolve this configure the foreign key properties explicitly on at least one of the relationships.

So in my DbContext I added these lines:

modelBuilder.Entity<Feed>().HasMany(m => m.Conversions).WithOne().HasForeignKey(m => m.FeedId);
modelBuilder.Entity<Feed>().HasMany(m => m.Filters).WithOne().HasForeignKey(m => m.FeedId);
modelBuilder.Entity<Feed>().HasMany(m => m.Rules).WithOne().HasForeignKey(m => m.FeedId);
modelBuilder.Entity<Feed>().HasMany(m => m.Transformations).WithOne().HasForeignKey(m => m.FeedId);

But I still get the same issue. Does anyone know what I can do to resolve this?

Upvotes: 2

Views: 2911

Answers (2)

Ryan
Ryan

Reputation: 20106

You need to move the public ICollection<Conversion> Conversions { get; set; } to avoid the error.

Since you use TPH which will uses a single table to store the data for all types in the hierarchy. A discriminator column is used to identify which type each row represents.

Then you could get the derived entities using below method:

public class SampleContext : DbContext
{
    public DbSet<Filter> Contracts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Conversion>().HasBaseType<Filter>();

    }
}

controller:

var conversions = _context.Filters.OfType<Conversion>().ToList();

Refer to https://www.learnentityframeworkcore.com/inheritance/table-per-hierarchy#convention

Upvotes: 2

David Browne - Microsoft
David Browne - Microsoft

Reputation: 88851

A Conversion already is a Filter, and so has a Feed. Either remove the Feed.Conversions navigation property, or remove the inheritance relationship between Conversion and Filter.

Upvotes: 0

Related Questions