-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Problem with sharing same table by two independent entities #11382
Comments
It works if I change public class DetailedUser
{
public Guid Id { get; set; }
public string Email { get; set; }
public virtual User User { get; set; } //Added a navigation property!
} And change the model building code like: protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>(b =>
{
b.ToTable("MyUsers");
b.Property(x => x.UserName).HasColumnName(nameof(User.UserName));
});
modelBuilder.Entity<DetailedUser>(b =>
{
b.ToTable("MyUsers");
b.Property(x => x.Email).HasColumnName(nameof(DetailedUser.Email));
b.HasOne(u => u.User)
.WithOne()
.HasForeignKey<User>(u => u.Id);
});
} However I don't want to make this since it makes |
Duplicate of #9005 |
I'm not sure if it's duplicate of #9005. |
When you say |
Hi,
I don't know if this feature is available by EF Core. However, I need to map two independent entity to same db table. Entity A will use some columns and Entity B will use some columns. They will share same Id. Also, they both may use some colums, or Entity A may be a subset of Entity B.
To demonstrate it, I prepared an example project here: https://github.com/hikalkan/samples/tree/master/EfCoreTableSharingDemo (you can just download and try).
Entities
DbContext
Migration code
It's creating the migration the way I expect:
Test Code
Exception
I'm getting the following exception message on
dbContext.SaveChanges();
:The stacktrace:
BTW, it can successfully query the existing data.
Additional Notes
I can not add navigation property from
User
toDetailedUser
. Because they are in different modules (project/assembly). But theDetailedUser
can have a reference to the assembly containing theUser
.You may wonder why it's needed and suspect that I'm designing it in a wrong way. But I'm creating a modular and extensible application framework and I need such a design.
So, please answer if it's possible and how. If not possible, do you consider such a feature request?
The text was updated successfully, but these errors were encountered: