-
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
Validate that no properties on an entity type are mapped to the same column #26263
Comments
@angelaki We're not really sure what you're trying to do here. Could you post the database schema and foreign key constraints that you are trying to map to? |
Hey @ajcvickers, if it's OK for you I'd first try just to explain it: I have a table where global object authorizations are stored. Therefor the (compound) primary key contains:
Now if any object uses these authorizations I join it via:
I'd like to define this part |
@angelaki It would really help if you posted post the database schema and foreign key constraints that you are trying to map to. |
I do not use a foreign key I only join the required data. I guess that's a bit special requirenment, but sure, I'll send a (simplified) scheme:
|
@angelaki We discussed this, but we don't think there is a way to map a relationship to these tables using EF Core, and it's not something we plan to add. I expect the Flitered Include you mention above is probably the best workaround. |
@ajcvickers I totally understand - it's a pretty specific use-case. But at least my approach got double checked by you, that's worth it, too 😉 |
With my workaround I'm now facing a new issue: when adding an entity I get a DbUpdateException because of a misformed SQL statement:
Any ideas for this? Or does my use-case force me to not add them to the object's collection but rather to the DbContext collection itself? |
@angelaki Try it on EF Core 6.0.0-rc2, if it still fails then open a new issue, including a minimal model. |
@AndriySvyryd yes, this issue still exists. I'll try to create a minimal model this week. |
Here is the minimal model. Did it now, since a fix the next time would be great, need it right now 😉
For the first exception no Db is needed. Here is the quite simple script for the Db the 2nd issue needs:
I'm already excited what you'll say! |
This call maps another property to the same column: modelBuilder.Entity<Authorization>().Property("MyObjectId").IsRequired().HasDefaultValue(Guid.Empty).HasColumnName("ObjectId"); Remove it and instead change the relationship to use the existing property as the foreign key: p.HasMany(p => p.Authorizations).WithOne().HasPrincipalKey(p => p.Id).HasForeignKey(p => p.ObjectId); |
@AndriySvyryd thank you! I didn't know that these methods can be combined! |
Hey, since I'm seeing there was a bit more activity on my issue - is this going to be a feature? 🥳 Just having the same issue again right now: I have a simple
Will something like |
I have a table where authorizations for multiple objects of my database are stored. Its primary key is
Now I'm forcing two issues here:
First I'd like to set the key like this:
p.HasMany(p => p.Authorizations).WithOne().HasPrincipalKey(p => new { Module = Module.Name, ObjectId = p.Id });
. This does not work since EFCore can only use entity properties for relations. So if I remove the Module it worksp.HasMany(p => p.Authorizations).WithOne().HasPrincipalKey(p => p.Id);
but I need to think about filtering them for every query / include.Furthermore it created a foreign key in the authorization tabe that I took care of by this:
I moved them both to the same column. Feels pretty hacky, but works. Do I use something here?
The text was updated successfully, but these errors were encountered: