Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:22:07+00:00 2026-05-26T02:22:07+00:00

I am trying to create a one-to-one relationship that is optional on one side

  • 0

I am trying to create a one-to-one relationship that is optional on one side between a User table and a UserInfo table. The specifications are that a UserInfo must have exactly one User, while a User can have one or zero UserInfos. Also we require that the foreign key exist in the UserInfo table so that the columns of the User table are not modified. We would like to use the relationship in C# LINQ-to-SQL, e.g., user.UserInfo.Email = "test@test.com",userInfo.User`, etc.

The T-SQL for the tables and the foreign key from UserInfos to Users is (roughly):

CREATE TABLE [dbo].[Users](
    [UserId] [int] IDENTITY(1,1) NOT NULL,
    [Username] [nvarchar](50) NOT NULL,
    CONSTRAINT [PK_dbo.Users] PRIMARY KEY CLUSTERED ( [UserId] ASC ),
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[UserInfos](
    [UserInfoId] [int] IDENTITY(1,1) NOT NULL,
    [UserId] [int] NOT NULL,
    [Email] [varchar](250) NOT NULL,
    CONSTRAINT [PK_UserInfo] PRIMARY KEY CLUSTERED ( [UserInfoId] ASC ),
    CONSTRAINT [UQ_UserId] UNIQUE NONCLUSTERED ( [UserId] ASC )
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[UserInfos]  WITH CHECK 
    ADD CONSTRAINT [FK_UserInfos.UserID_Users.UserId] FOREIGN KEY([UserId])
    REFERENCES [dbo].[Users] ([UserId])
GO

ALTER TABLE [dbo].[UserInfos] CHECK CONSTRAINT [FK_UserInfos.UserID_Users.UserId]
GO

The problem is that if I define a foreign key from Users.UserId (the primary key) to UserInfos.UserId (which is, I understand, the correct way to define a non-optional one-to-one relationship) then performing the LINQ-to-SQL code user.UserInfo = null also sets the user.UserId to default(int).

Here is the T-SQL I use to define the foreign key between Users and UserInfos:

ALTER TABLE [dbo].[Users]  WITH CHECK 
    ADD CONSTRAINT [FK_Users.UserId_UserInfos.UserId] FOREIGN KEY([UserId])
    REFERENCES [dbo].[UserInfos] ([UserId])
GO

ALTER TABLE [dbo].[Users] CHECK CONSTRAINT [FK_Users.UserId_UserInfos.UserId]
GO

If I do not define this foreign key, then I get no LINQ-to-SQL property on a User allowing me to access the UserInfo. How can I have a relationship between the table Users and UserInfos that is traversable with LINQ-to-SQL, while at the same time allowing this relationship to be null from the User side? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:22:08+00:00Added an answer on May 26, 2026 at 2:22 am

    You can do this but you’re approaching it from the wrong side.

    When you call

    user.UserInfo = null;
    

    the exception states that

    An attempt was made to remove a relationship between a User and a UserInfo. However, one of the relationship’s foreign keys (UserInfo.UserId) cannot be set to null.

    LINQ-to-SQL thinks you are trying to remove the relationship between the two items; and in an indirect way you are. But the exception is just a reiteration of what you’ve stated yourself – a UserInfo must have a User. You’re unlinking the two objects but you aren’t removing the UserInfo – you’re leaving an orphaned UserInfo in your context which cannot be persisted to the database (due to the foreign key constraint).

    The answer? Delete the UserInfo.

    var user = context.Users.First();
    var userinfo = user.UserInfos;
    user.UserInfos = null;
    context.UserInfos.DeleteOnSubmit(userinfo);
    context.SubmitChanges();
    
    // or, even simpler:
    var user = context.Users.First();
    context.UserInfos.DeleteOnSubmit(user.UserInfos);
    context.SubmitChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to create a table that has a one to many relationships.
I have a data table that contains a one-to-many self referential relationship: Plant {
I'm trying to write a route that captures the one-to-many relationship between posts and
I'm trying to create a table with Listview and one of the fields I'm
this one is really easy. I'm trying to create a Regular Expression that will
I am trying to relate 2 items. I have a table that is simply
I'm trying to implement a hasone relationship between 2 models, but I can't have
I am trying to create an intermediate model between two models that I'd like
Possible Duplicate: MySQL Relationships I am trying to create a one to many relationship
I'm using Entity Framework 4.1 and trying to create a one-to-many relationship. In my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.