Skip to content
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

[Inheritence] Setting base type can merge 2 different relationships #3289

Closed
smitpatel opened this issue Oct 1, 2015 · 4 comments
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@smitpatel
Copy link
Contributor

When base type and derived type both have relationship with one entity and if the relationships use same foreignkey properties (configured by conventions) then while setting base type, relationship from derived type is removed even though both relationships are different (navigationToDependent is different)

Repro:

public class AnEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public BaseReference BaseReference { get; set; }
        public DerivedReference DerivedReference { get; set; }
    }

    public class BaseReference
    {
        [NotMapped]
        public int Id { get; set; }
        public string Name { get; set; }

        public int? BaseParentId { get; set; }
        [NotMapped]
        public AnEntity BaseParent { get; set; }
    }

    public class DerivedReference : BaseReference
    {
    }

    public class MyContext : DbContext
    {
        public DbSet<AnEntity> DerivedEntities { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                @"Server=(localdb)\MSSQLLocalDB;Database=_TestApp6;Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=30");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AnEntity>().HasOne(e => e.BaseReference).WithOne();
            modelBuilder.Entity<AnEntity>().HasOne(e => e.DerivedReference).WithOne();

            modelBuilder.Entity<AnEntity>().HasKey(e => e.Id);

            modelBuilder.Entity<DerivedReference>().BaseType<BaseReference>();

            modelBuilder.Entity<BaseReference>().HasKey(e => e.Id);
        }
    }
Console.WriteLine(context.Model.GetEntityType(typeof(BaseReference)).GetForeignKeys().Count());
Console.WriteLine(context.Model.GetEntityType(typeof(DerivedReference)).GetForeignKeys().Count());

//expected output
1
2
//actual output
1
1
@rowanmiller rowanmiller added this to the 7.0.0-rc1 milestone Oct 2, 2015
@rowanmiller rowanmiller modified the milestones: 7.0.0, 7.0.0-rc1 Oct 14, 2015
@smitpatel
Copy link
Contributor Author

This does not repro anymore 😌
@AndriySvyryd - This was reported during beta 8 when we did not have BaseTypeDiscoveryConvention. In latest code, base type is set even before relationships are configured. Any test scenario to cover this?

@AndriySvyryd
Copy link
Member

We don't have many tests for relatinships with inheritance and NotMapped, so you can just add this repro as a test

@smitpatel
Copy link
Contributor Author

Just now remembered this is from our test fixture only.
https://github.com/aspnet/EntityFramework/blob/788aee079f6aec2b077113f18cec82322da09336/test/Microsoft.EntityFrameworkCore.FunctionalTests/InheritanceRelationshipsQueryFixtureBase.cs#L63

I will remove work-around to see if it works else add test in model builder tests

@AndriySvyryd
Copy link
Member

You can add tests that use the same model, but Assert that the model shape is correct

@smitpatel smitpatel modified the milestones: 1.0.0-rc2, 1.0.0 Mar 11, 2016
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants