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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:11:24+00:00 2026-06-03T21:11:24+00:00

I have this domain: public class Phone { public int Id { get; set;

  • 0

I have this domain:

public class Phone {
    public int Id { get; set; }
    public string Number { get; set; }
    public Person Person { get; set; }
}
public class Person {
    public int Id { get; set; }
    public IList<Phone> Phones { get; set; }
}

I load a Person and clear its Phones. But the operation cause an error:

// actually loads person from repository...
var person = _personRepository.Include(p => p.Phones).Where(p => p.Id == 1).First();
person.Phones.Clear();
_personRepository.Update(person);

Above you can see the simpled logic of loading a Person and clearing its Phones. But this error occurs:

The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.

Actually I want to clear all of Person.Phones and add some new items. But I want to clearing them in one query, not delete them one by one.

Have you any idea? Can you help me please? Thanks in advance.

  • 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-03T21:11:25+00:00Added an answer on June 3, 2026 at 9:11 pm

    You can’t generate set based SQL in EF. So there’s no way in EF to generate a single SQL statement that deletes all Phone records given a Person.Id.

    You can write the SQL yourself and pass it to either ObjectContext.ExecuteStoreCommand or DbContext.Database.ExecuteSqlCommand depending on your model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have this domain object... public class SpansMultipleTables { public int CommonID {get;
I have a domain class like this: public class DomainClass { public virtual string
suppose I have a domain classes: public class Country { string name; IList<Region> regions;
I have this: public static class Domain { private const string sessionKey = NHib.SessionKey;
I have the following Domain Object: public class DomainClass { public int Id {
I have this: namespace Demo.Framework.Domain { public class UserEntity { public virtual Guid UserId
I have the following domain model: public class Name { private readonly string fullName;
I have a standard Domain Layer entity: public class Product { public int Id
Let's assume I have the following domain: public class Movie { public string Id
I have a class in my domain model root that looks like this: namespace

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.