Page 1 of 1

One to zero/one relation

Posted: Sat 24 Jun 2017 10:42
by jimbeats
Hi,

how can i do this (following image) in entity developer with DbContext and Entity framework 6

best regards
jimmy

Image

Re: One to zero/one relation

Posted: Fri 30 Jun 2017 13:35
by Shalex
We will investigate the question and notify you about the result as soon as possible.

Re: One to zero/one relation

Posted: Fri 30 Jun 2017 14:12
by Shalex
This is the Entity Framework engine issue:
http://stackoverflow.com/questions/2141 ... 59#2326859
http://stackoverflow.com/questions/4144 ... ork-4?lq=1

Here is a way of creating 1 : 0…1 association with Entity Developer:
1. Create a database schema:

Code: Select all

CREATE TABLE [dbo].[Student](
	[Student_ID] [bigint] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_Student] PRIMARY KEY (Student_ID)
)
GO

CREATE TABLE [dbo].[StudentContact](
	[StudentContact_ID] [bigint] IDENTITY(1,1) NOT NULL,
	[ContactNumber] [nvarchar](max) NOT NULL,
	[Student_ID] [bigint] NULL,
 CONSTRAINT [PK_StudentContact] PRIMARY KEY (StudentContact_ID)
)
GO

ALTER TABLE [dbo].[StudentContact]  WITH CHECK ADD  CONSTRAINT [FK_StudentContact_Student] FOREIGN KEY([Student_ID])
REFERENCES [dbo].[Student] ([Student_ID])
GO
2. Add these tables to the Entity Framework Model using Create Model Wizard.
3. Delete the StudentID foreign key property from StudentContact entity in the Designer.
4. Right-click the association and select Mapping Details from the context menu.
5. In the Mapping Details window, click the Mapped Entity drop-down and select StudentContact.
6. Add a condition to StudentContact: the Student_ID column with the IsNotNull operator. Click OK.
7. Select the Association between Student and StudentContact.
8. In the Properties window for the association, in the property called “End2”, which currently has the value StudentContacts(*), change its Multiplicity property to 1 (One) using its drop-down list.
9. Validate the model by right-clicking the design surface and choosing Validate.

For the 1..1 association there is no need to set the "Student_ID IsNotNul" condition (steps 4 through 6) because the column itself is not nullable in this case.

JIC:
* enable DbContext template in your model and set its Fluent Mapping property to True
* select diagram and set Metadata Artifact Processing = Do Not Generate Mapping Files in the properties of EntityContextModel

Re: One to zero/one relation

Posted: Fri 23 Jul 2021 04:46
by rei
Is this still the only way to create a zero-one relation?

Re: One to zero/one relation

Posted: Mon 26 Jul 2021 11:25
by Shalex
rei wrote: Fri 23 Jul 2021 04:46 Is this still the only way to create a zero-one relation?
Yes, this is the only way.