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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:45:59+00:00 2026-06-14T14:45:59+00:00

I want to update a record type graph, which modify the parent object and

  • 0

I want to update a record type graph, which modify the parent object and child objects also having internally. The problem usually occurs when EF UPDATE registry for children puts them to NULL in where children helps identify the records you are updating.

My domain classes are:

This class helps me to change the status, Add – Customized – delete, which is the graph from the parent as all children. The function that makes it work.

public interface IObjectWthState
{
    State State { get; set; }
}
public enum State 
{
    Added,
    Unchanged,
    Modified,
    Deleted
}

This is a User Class:

 public abstract class User : IObjectWthState 
 {
    public int Id { get; set; }
    public String Name { get; set; }
    public String LastName { get; set; }        
    [Timestamp]
    public byte[] RowVersion { get; set; }
    [NotMapped]
    public State State { get; set; }
}

And here are the two classes that inherit from user:

public class AdminUser:User
{
    public ICollection<BasicUser> UsersList { get; set; }
    public String Email { get; set; }
}

public class BasicUser: User
{
    public String Position { get; set; }
    public String Department { get; set; }
}

As seen AdminUser BasicUser has a list.

The model was generated as wanted, needed to detect a foreign key and add. Here a picture of the DB:

Model generated with migration

This is the function that adds or updates the infromación:

public virtual void AddUpdateGraph(T entity)
{

    if (((IObjectWthState)entity).State == State.Added)
    {
        DbEntityEntry dbEntityEntry = dbContext.Entry(entity);
        dbEntityEntry.State = EntityState.Added;
    }
    else
    {
        dbSet.Add(entity);
        dbContext.ApplyStateChanges();
    }
} 

The function that handles go and adjust the status of internal nodes:

public static void ApplyStateChanges(this DbContext context)
{
    foreach (var entry in context.ChangeTracker.Entries<IObjectWthState>())
    {
        IObjectWthState stateInfo = entry.Entity;
        entry.State = StateHelpers.ConvertState(stateInfo.State);
    }
}  

Function that returns the state to EF:

public static EntityState ConvertState(State state)
{
    switch (state)
    {
        case State.Added:
            return EntityState.Added;
        case State.Modified:
            return EntityState.Modified;
        case State.Deleted:
            return EntityState.Deleted;
        default:
            return EntityState.Unchanged;
    }
}

When you want to add a new AdminUser with his list BasicUser everything works fine with no problem, the problem comes when you want to modify the EF BasicUser AdminUser and generates updates for the BasicUser but added a condition that the foreign key is null.

Here you can see the two updates that are generated

AdminUser:

 exec sp_executesql N'update [dbo].[User]
set [Name] = @0, [LastName] = @1, [Email] = @2
where (([Id] = @3) and ([RowVersion] = @4))
select [RowVersion]
from [dbo].[User]
where @@ROWCOUNT > 0 and [Id] = @3',N'@0 nvarchar(max) ,@1 nvarchar(max) ,@2 nvarchar(max) ,@3 int,@4 binary(8)',@0=N'Beto',@1=N'Guerere',@2=N'beto@gmail.com',@3=3,@4=0x0000000000000801

BasicUser:

exec sp_executesql N'update [dbo].[User]
set [Name] = @0, [LastName] = @1, [Position] = @2, [Department] = @3, [AdminUser_Id] = @4
where ((([Id] = @5) and ([RowVersion] = @6)) and [AdminUser_Id] is null)
select [RowVersion]
from [dbo].[User]
where @@ROWCOUNT > 0 and [Id] = @5',N'@0 nvarchar(max) ,@1 nvarchar(max) ,@2 nvarchar(max) ,@3 nvarchar(max) ,@4 int,@5 int,@6 binary(8)',@0=N'Viomar',@1=N'Guerere',@2=N'Supervisora',@3=N'Ventas',@4=3,@5=4,@6=0x0000000000000802

As you see in the EF generated SQL command to do the update of BasicUser is added the condition of the record has the Null value to a AdminUser_Id. I do not understand the reason for this. The field can not be null because that user is assigned to a supervisor.

I hope I explained.

Thank you very much for any help you can give me.

  • 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-14T14:46:00+00:00Added an answer on June 14, 2026 at 2:46 pm

    Answering my question to me: EF believes the FK in the database anyhow you have to work with her inside the model as variable of this form if you work disconnected you be to be sure that this your information being stored.

    The first thing that has to be done is to add in the class BasicUser a variable of type int the one that we will call AdminUser_Id:

    public class BasicUser: User
        {       
            public String Position { get; set; }
            public String Department { get; set; }
            public int AdminUser_Id { get; set; }
        }
    

    Then we have to tell EF that the variable added is a FK that references a AdminUser AdminUser and can have many BasicUser.

    To do it, we go where we are declaring the contex, DataLayer (In My case), and in the cuncion where this being created the model we add the following annotation:

    modelBuilder.Entity<AdminUser>()HasMany(A => A.UsersList ).WithRequired().HasForeignKey(B => B.AdminUser_Id );
    
    We update the model and with these adjustments the mistake is eliminated completely at the moment of realizing the update of the record.
    

    For more information about FK and EF recommend reading this article:

    1. Making Do with Absent Foreign Keys
    2. Code First Relationships Fluent API

    Thank you very much to all especially to Julie Lerman for your help, without it I would have cost a little more to get to that solution.

    I hope I have explained.

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

Sidebar

Related Questions

I want to have an 'updateinfo' table in order to record every update/insert/delete operations
i want update textarea content onclock on a input button for example: <input type=button
I want to update a record in a table but based on a condition
I have a DataGridView object to which I've bound a list of objects (of
I want to convert the record [(1, 2, 3) (2, 2, 3)] which is
i want to update all records in my database(mongodb),I tried to use command below
I have a dataset with some 30 records in it. I want to update
I want a validation to run before a record gets updated. I know of
What I want: Update all new commits from server with my local repository in
i have ios application with xcdatamodeld, i want update some data so need a

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.