1
votes

I'm trying to implement a relationship(one-to-many) for my entities which use default TPH inheritance

public abstract class base
{
        public int Id { get; set; }
        ...
}

public class X : base
{
        public ApplicationUser User { get; set; }
       ...
}

public class Y : base
{
        public ApplicationUser User { get; set; }
       ...
}

public class ApplicationUser
{
        public string Name { get; set;}
       ...
        public ICollection<X> classX { get; set; }
        public ICollection<Y> classY { get; set; }
}

Everything works, but the problem is that Entity Framework creates two columns in the base table - User_Id and User_Id1. How can I map it so that there is only one column for the foreign key (User_Id) and depending on the content of the record in the Discriminator column (created by EF) the foreign key would be assigned to the appropriate entity?

1
Hey. Just an in general comment. "base" is a keyword. So its not an optimal name for a base class. "MyBaseClass" would be a better candidate for sample code. And real code..I would never name a class that. Does that make sense? docs.microsoft.com/en-us/dotnet/csharp/language-reference/… - granadaCoder

1 Answers

0
votes

How can I map it so that there is only one column for the foreign key (User_Id) and depending on the content of the record in the Discriminator column (created by EF) the foreign key would be assigned to the appropriate entity?

You can't. Thinking about it a bit I don't see any obvious reason why that feature couldn't be implemented. It just hasn't.

If you want X and Y to share an attribute, derive them both from and intermediate XYEntity type, and give ApplicationUser a single Navigation Property of type ICollection<XYEntity>.