One to zero/one relation

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
jimbeats
Posts: 1
Joined: Sat 24 Jun 2017 10:35

One to zero/one relation

Post by jimbeats » Sat 24 Jun 2017 10:42

Hi,

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

best regards
jimmy

Image

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: One to zero/one relation

Post by Shalex » Fri 30 Jun 2017 13:35

We will investigate the question and notify you about the result as soon as possible.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: One to zero/one relation

Post by Shalex » Fri 30 Jun 2017 14:12

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

rei
Posts: 10
Joined: Wed 21 Jul 2021 20:07

Re: One to zero/one relation

Post by rei » Fri 23 Jul 2021 04:46

Is this still the only way to create a zero-one relation?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: One to zero/one relation

Post by Shalex » Mon 26 Jul 2021 11:25

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.

Post Reply