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 3948174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:20:35+00:00 2026-05-20T01:20:35+00:00

I have a table Location that has a foreign key relationship to a Site.

  • 0

I have a table “Location” that has a foreign key relationship to a “Site”. I am using linq to sql to try and add a new location record:

Location l = new Location
                 {
                     Description = text,
                     FloorId = 0,
                     IsCurrentLoc = false,
                     LastMove = DateTime.Now,
                     MoveId = 0,
                     SiteId = 1 
                 };

LocationTable.InsertOnSubmit(l);
LocationTable.Context.SubmitChanges();

However when I try to save the location row I see this error:

An attempt was made to remove a relationship between a Site and a Location. However, one of the relationship's foreign keys (Location.SiteId) cannot be set to null.

I’ve set the location siteid (site id 1 exists in the database).

My linq-to-sql classes look like this:

[Table(Name = "Locations")]
public class Location
{

    private List<InstallLocation> _InstallLocations = new List<InstallLocation>();


    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    internal int LocationId { get; set; }

    [Association(ThisKey = "SiteId", OtherKey = "SiteId", IsForeignKey = true)]        
    public Site Site
    {
        get;
        set;
    }

    [AssociationAttribute(ThisKey = "LocationId", OtherKey = "LocationId")]
    public List<InstallLocation> InstallLocations
    {
        get
        {
            return this._InstallLocations;
        }
        set
        {
            this._InstallLocations = value;
        }
    }

}

EDIT – So I know why this happens but not how to fix it…

Thanks to this post I now see what is happening. The “Site” property takes presendese over SiteId. Since I don’t set Site to anything it tries to set SiteId to null – which is not allowed hence it fails.

What I can’t quite understand is how to work with this. I don’t want to load up the Site entity from the database and take a DB hit just to set the Site. I have the SiteId and that is all I should need in order to persist my “Location”.

Any ideas?

  • 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-20T01:20:36+00:00Added an answer on May 20, 2026 at 1:20 am

    Found the solution for this.

    Basically, my original posting was close. What I needed to do was to allow my Foreign key property to be nullable in my class. This seems counter-intuitive since its not nullable in the DB, but L2S will set this to null before setting it to the correct value later..

    Full working code looks like this, showing how Location is associated with Site:

    [Table(Name = "Locations")]
    public class Location
    {
    
        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        internal int LocationId { get; set; }
    
        [Column]        
        public int? SiteId { get; set; }
    
        [Association(ThisKey = "SiteId", OtherKey = "SiteId", IsForeignKey = true)]        
        public Site Site
        {
            get;
            set;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables: unmatched_purchases table: unmatched_purchases_id --primary key purchases_id --foreign key to events
I have a parent/child relationship defined which, unfortunately, cannot be maintained via a foreign
I have a database that consists of 5 tables : Course, Category, Location, CourseCategories,
I have a table whose primary key is used in several other tables and
We are using ASP.NET MVC with LINQ to SQL. We added some features and
I have a database that I would like to leverage with Zend_Search_Lucene . However,
I have many tables that use Lookup/Enum references for most of their column values.
I have a simple database with multiple tables. I can't figure out how to
I get an error when running the following create table: CREATE TABLE Event (
I dont really get to work with entity beans, but they are staring at

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.