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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:23:50+00:00 2026-06-12T06:23:50+00:00

I have a Person table that has a nullable AddressId in it which is

  • 0

I have a Person table that has a nullable AddressId in it which is foreign keyed to an Address table. So zero..one to many.

Using EF and ObjectContext I can call person.Address to access the Address object for a person. Neat!

Look at this fairly simple code then I’ll tell you my issue:

var _db = new DataContext();
Person person = _db.getThatOneGuy();

//some changes are done to the person object
person.FirstName = "Harry";

//If there's no address I want to make one
if(person.Address == null)
person.Address = new Address();

person.Address.StreetOne = "blah";
person.Address.StreetTwo = "blah";

//I decide I don't actually want this new address
person.Address = null;

_db.SaveChanges();

The call to SaveChanges() will still generate an sql UPDATE (firstname = ‘Harry’) which is great BUT it also generates an sql INSERT statement trying to add that new address (blah, blah). (I know because I checked the generated SQL in sql profiler) It errors because I have fields like StateId that are not nullable, but I don’t want it to even try to add that address (my attempt to stop it was by making it null) Help me please.

Note: I have before been successful with collections (many to many relationships) where I could do person.Addresses.Add(addressObj) then person.Addresses.Remove(addressObj) and the ObjectContext knows not to do an insert…. just not sure how to handle this 0..1 to many address situation.

Thanks.

  • 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-06-12T06:23:51+00:00Added an answer on June 12, 2026 at 6:23 am

    The problem is that setting Address property to null does not remove Address instance from the context. It only informs context that there is no relation between Person instance and Address instance. You still have to remove Address instance from the context. Try this:

    _db.Detach(person.Address);
    _db.SaveChanges();
    

    or:

    var address = person.Address; 
    person.Address = null;
    _db.Detach(address);
    _db.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Entity Framework. I have a table Person that has a foreign
I have a database structure that has a Person table which contains fields such
I have a table that has a column which holds the id of a
I have a table that stores two foreign keys, implementing a n:m relationship. One
i have a table that has the following columns: Team Region Person Name and
I have three table in SQL database such as table Person has relation one
Using this as a simplified example, assume I have a table that has some
I have a Person DB table that has a surname , name , and
I have an Person Table that has customers and salesman. Each customer is assigned
I have a table Person that has 3 columns: Id, Name, ParentId where ParentId

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.