Page 1 of 1

One to zero/one using same key on both tables

Posted: Fri 23 Jul 2021 02:57
by rei
I have a table, Contacts, and a table, Login.

A Contact may or may not have a Login.

I'm using 64 bit IDs generated in the service rather than in the DB, so in theory, I should be able to use the same IDs for both tables.

The problem is that Entity Developer models this as a 1:1 relationship, which kind of makes sense, since it has no way of knowing which of the two tables is the "main" one. So the navigation property from Contact to Login -- which should be nullable -- isn't.

I could have Login inherit from Contact instead, but that's not quite what I want, as it won't produce a navigation property.

And in theory, I could just have a foreign key on Login as opposed to a primary key, but EF doesn't condone that, and ED doesn't allow it.

Is there a way around this?

Thanks!

Re: One to zero/one using same key on both tables

Posted: Fri 23 Jul 2021 05:04
by rei
Actually, it doesn't seem possible to make a one to zero/one relationship unless the foreign key is nullable.

I must be missing something obvious...

Re: One to zero/one using same key on both tables

Posted: Mon 26 Jul 2021 11:33
by Shalex
You are working with EF6, aren't you? Refer to viewtopic.php?f=32&t=35595.

Re: One to zero/one using same key on both tables

Posted: Tue 10 Aug 2021 03:36
by rei
EF Core. This seems to have worked, but I'm still running into problems getting nullable reference types (specifically, 1-0 keys) to actually be nullable, even with it turned on in the template.

Re: One to zero/one using same key on both tables

Posted: Wed 11 Aug 2021 12:26
by Shalex
With EF Core, create a one-to-one association, map its FK end to the property with Nullable=True to make the association 1-0..1.

Re: One to zero/one using same key on both tables

Posted: Wed 18 Aug 2021 04:42
by rei
Image

I'm trying to make this a 0..1 with the Login being optional. Neither the FK ContactID nor the navigation property are nullable in this situation, and I can't make ContactID a non-PK without it complaining (which makes sense).

Ideally, I'd like not to make a LoginId FK in Contact. I'm able to do this in code, but I don't seem to be able to in ED.

Re: One to zero/one using same key on both tables

Posted: Wed 18 Aug 2021 19:01
by Shalex
We will investigate the behavior of the Constrained option in Association Editor of EF Core Model. Please specify the exact version of your EF Core.

As a workaround, please redesign your Login table to create LoginId (PK) and ContactId (FK, Nullable=True).

Re: One to zero/one using same key on both tables

Posted: Thu 19 Aug 2021 00:15
by rei
We're using EF Core 5.0.9.

Thanks!

Re: One to zero/one using same key on both tables

Posted: Thu 19 Aug 2021 18:51
by Shalex
We will notify you about the results of our investigation.

Re: One to zero/one using same key on both tables

Posted: Thu 19 Aug 2021 21:54
by rei
I just realized that it doesn't make any of the navigation properties nullable, even if it's based on a FK (i.e. not an inverse property).

Re: One to zero/one using same key on both tables

Posted: Wed 01 Sep 2021 11:35
by Shalex
  • The bug with code generation for the Constrained option of Association Editor in the case of 0..1 -> 1 association in EF Core Model is fixed
  • The representation of one-to-one association with non-nullable properties on both ends is changed from 1 -> 1 to 0..1 -> 1 on a diagram in EF Core Model
We will notify you when a new public build of Entity Developer is available for download.

Re: One to zero/one using same key on both tables

Posted: Sat 02 Oct 2021 07:55
by rei
Thanks!

Re: One to zero/one using same key on both tables

Posted: Mon 25 Oct 2021 22:36
by rei
Has this been addressed? It looks like it's still treating the navigation property as non-null.

Edit: Actually, even for a normal many-to-one navigation property with a nullable ID, the navigation property ends up non-nullable.

Re: One to zero/one using same key on both tables

Posted: Wed 03 Nov 2021 10:43
by Shalex
Sorry for the late response. We are processing your request.