-
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
Optional alternate key properties #4415
Comments
Agreed this would be good. Currently EF is built on the assumption that an alternate key is always populated, but we could definitely relax this. Putting on backlog since we won't be doing this for 1.0.0. |
Keywords: nullable alternante keys. |
Note: allowing alternate keys to be nullable is potentially not difficult--from an EF perspective, if an entity has a null alternate key, then it's the same as that entity not existing for any dependents. Note, however that making alternate keys read-write is much more involved. |
@ajcvickers how if i need to use in a relationship, is it possible or not the right way ? |
Hi, |
@buttch Currently EF can't handle rows with a null principal key value. One workaround is to write your queries such that they filter out any principal entities with null values. That is, manually add a |
@ajcvickers Thanks for answer. Unfortunately your workaround it not good for me. This null principal (alternate) key value is in dependent of dependent of dependent of my main entity and I need those entities anyway I cannot filter them out. I think in such case the only way is to load them in separate query and add to entities manually. What do you think ? |
@buttch First, make sure you really need an alternate key in EF terms. You said, "it turned out that in columns which I use for relation are null in same case" which I read to mean that these columns are referenced at the principal end of a relationship. If that's not the case, then configure a unique constraint instead of an alternate key as mentioned in the tip here. Beyond that, I'm afraid EF can't track entities with null alternate key values. Unfortunately, I can't think of any other workarounds. |
Please thumbs up the original post so that it has a better chance of being considered in the Plan for Entity Framework Core 5.0.
|
Hi @BradBarnich Can you please suggest how exactly you did the workaround? I have something like this.
My Order entity has OrderId as Please help! |
Why is this limitation there? 😔 I simply want a unique constraint where a few columns are nullable, but that's OK. It should behave the same way as a unique index without a filter, which it would do if this worked. |
@ajcvickers But is there a way to create a unique constraint without an alternate key (as opposed to a unique index)? The documentation you pointed to doesn't show anything like that. |
@Neme12 No. Can you explain why you need it to be a constraint instead of an index? Are you aware of a database that implements a unique constraint and a unique index differently? |
So just to confirm, currently, EF Core 3.0 does not support nullable composite foreign keys/alternate keys, correct? I've got this code in my context, however I still get the foreign key generated as not nullable :(
|
@rp0m Nullable foreign keys are supported and commonly used. Alternate keys cannot be nullable. |
It also seems to be assuming that I have an alternate key when I really don't want it to be an alternate key. Not sure how to fix that. |
+1 on requesting this feature. It would be very useful for us. |
@ajcvickers EF Core v6.0.3: I am facing some weird issue on an entity with Alternate Key. The AlternateKey column is nullable. When I load the entity along with the AlternateKey navigation property using |
You're right, I don't need it, I'd just prefer it, and if it was possible, I wouldn't need a separate property as a primary key - I could simply use a composite key from 2 columns where one or both are nullable (and where null should be a unique value too). |
+1 on requesting this feature highly useful |
+1 |
+1, I'm currently migrating a system from NHibernate and I have stumbled upon this problem. |
Can you please tackle this in EF9 as part of the work to support nullable complex types? Both these features become nearly useless when they can be only applied to non-nullable types/fields. It's a complete disconnect with real domains in the wild, which in practice tend to have MOST fields be nullable / vary by some related attribute, and there is very little you can guarantee will never be null (ie: it's highly academic to design an ORM and the systems they power against such assumptions). UPDATE: It seems unique indexes don't work with nullable columns either. This really needs to be tackled as a cross-cutting epic: "Make Nullable Columns Work (everywhere)". |
Just hit this snag and having nullable alternate keys would be lovely. Looking into workarounds now |
+1 |
+1 |
All of the major relational databases allow null values in unique key constraints.
As a workaround I could use unique indexes instead of alternate keys, but I would appreciate the semantics of a unique key constraint.
Something like this got me around the issue: BradBarnich@c23988e
I can fix it up and submit a PR if you agree this should be fixed.
The text was updated successfully, but these errors were encountered: