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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:50:00+00:00 2026-05-18T19:50:00+00:00

I’m literally going crazy with this. I receive from the HTML the data of

  • 0

I’m literally going crazy with this. I receive from the HTML the data of one “Father” entity, along with the data of three child entities.

In my modelbinder, I create stub entities for relationships, containing only the primary key.

This is my ModelBinder code:

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        DefaultModelBinder binder = new DefaultModelBinder();
        var estimate = (Estimate)binder.BindModel(controllerContext, bindingContext);

        estimate.Id = Guid.NewGuid();
        estimate.OwnerSociety = ModelBinderHelper.MapComplexType<OwnerSociety, int>(controllerContext, "OwnerSociety", int.Parse, c => c.IDOwnerSociety);
        estimate.EstimateType = ModelBinderHelper.MapComplexType<EstimateType, Guid>(controllerContext, "EstimateType", Guid.Parse, c => c.Id);
        estimate.Brand = ModelBinderHelper.MapComplexType<Brand, int>(controllerContext, "Brand", int.Parse, c => c.IDBrand);
        estimate.FromAccount = ModelBinderHelper.MapComplexType<User, int>(controllerContext, "FromAccount", int.Parse, c => c.IDUser);
        estimate.ManagerDirector = ModelBinderHelper.MapComplexType<User, int>(controllerContext, "ManagerDirector", int.Parse, c => c.IDUser);

        estimate.Projects.Add(new Project
        {
            StrategicPlanner = ModelBinderHelper.MapComplexType<User, int>(controllerContext, "StrategicPlanner_1", int.Parse, c => c.IDUser),
            Activity = ModelBinderHelper.MapComplexType<Activity, int>(controllerContext, "Activity_1", int.Parse, c => c.IDActivity),
            ProjectState = ModelBinderHelper.MapComplexType<ProjectState, int>(controllerContext, "ProjectState_1", int.Parse, c => c.IDProjectState),
            StartDate = DateTime.Now,
            Name = "XXX1",
            Brand = estimate.Brand,
            ProjectTypes = new ProjectTypes { IDProjectType = 1 },
            Consuntivo = 0,
            Order = 1
        });
        estimate.Projects.Add(new Project
        {
            StrategicPlanner = ModelBinderHelper.MapComplexType<User, int>(controllerContext, "StrategicPlanner_2", int.Parse, c => c.IDUser),
            Activity = ModelBinderHelper.MapComplexType<Activity, int>(controllerContext, "Activity_2", int.Parse, c => c.IDActivity),
            ProjectState = ModelBinderHelper.MapComplexType<ProjectState, int>(controllerContext, "ProjectState_2", int.Parse, c => c.IDProjectState),
            StartDate = DateTime.Now,
            Name = "XXX2",
            Brand = estimate.Brand,
            ProjectTypes = new ProjectTypes { IDProjectType = 1 },
            Consuntivo = 0,
            Order = 2
        });
        estimate.Projects.Add(new Project
        {
            StrategicPlanner = ModelBinderHelper.MapComplexType<User, int>(controllerContext, "StrategicPlanner_3", int.Parse, c => c.IDUser),
            Activity = ModelBinderHelper.MapComplexType<Activity, int>(controllerContext, "Activity_3", int.Parse, c => c.IDActivity),
            ProjectState = ModelBinderHelper.MapComplexType<ProjectState, int>(controllerContext, "ProjectState_3", int.Parse, c => c.IDProjectState),
            StartDate = DateTime.Now,
            Name = "XXX3",
            Brand = estimate.Brand,
            ProjectTypes = new ProjectTypes { IDProjectType = 1 },
            Consuntivo = 0,
            Order = 3
        });

        return estimate;
    }

When I try to simply attach my father entity, declare it and the child properties as Added, I get the “there is already an object in the objectstatemanager with the same key”. If I try to insert the father entity without childs, it works.

I tried another “way”. The following code:

public Estimate CreateEstimate(Estimate toCreate)
    {
        var brand = Brands.First(c => c.IDBrand == toCreate.Brand.IDBrand);
        var estimateType = EstimateTypes.First(c => c.Id == toCreate.EstimateType.Id);
        var account = Users.First(c => c.IDUser == toCreate.FromAccount.IDUser);
        var manager = Users.First(c => c.IDUser == toCreate.ManagerDirector.IDUser);
        var owner = OwnerSocieties.First(c => c.IDOwnerSociety == toCreate.OwnerSociety.IDOwnerSociety);

        toCreate.Brand = brand;
        toCreate.EstimateType = estimateType;
        toCreate.FromAccount = account;
        toCreate.ManagerDirector = manager;
        toCreate.OwnerSociety = owner;

        foreach (var project in toCreate.Projects)
        {
            project.Activity = Activities.First(c => c.IDActivity == project.Activity.IDActivity);
            project.ProjectState = ProjectStates.First(c => c.IDProjectState == project.ProjectState.IDProjectState);
            project.StrategicPlanner = Users.First(c => c.IDUser == project.StrategicPlanner.IDUser);
            project.ProjectTypes = _entities.ProjectTypes.First();
        }

        _entities.EstimateSet.AddObject(toCreate);

        return toCreate;
    }

But it doesn’t work, telling me that “INSERT on table “Activities” fails because column “Name” can’t be null”. But it really shouldn’t be inserting anything in the Activity table, in fact I’m retrieving the Activity items from the DB and using them.

Funny, the following code works:

public Estimate CreateEstimate(Estimate toCreate)
    {
        var estimate = new Estimate();

        var brand = Brands.First(c => c.IDBrand == toCreate.Brand.IDBrand);
        var estimateType = EstimateTypes.First(c => c.Id == toCreate.EstimateType.Id);
        var account = Users.First(c => c.IDUser == toCreate.FromAccount.IDUser);
        var manager = Users.First(c => c.IDUser == toCreate.ManagerDirector.IDUser);
        var owner = OwnerSocieties.First(c => c.IDOwnerSociety == toCreate.OwnerSociety.IDOwnerSociety);

        estimate.Id = toCreate.Id;
        estimate.Brand = brand;
        estimate.EstimateType = estimateType;
        estimate.FromAccount = account;
        estimate.ManagerDirector = manager;
        estimate.OwnerSociety = owner;
        estimate.Date = toCreate.Date;
        estimate.Subject = toCreate.Subject;
        estimate.Status = toCreate.Status;
        estimate.Language = toCreate.Language;

        foreach (var project in toCreate.Projects)
        {
            var project1 = new Project();
            project1.Activity = Activities.First(c => c.IDActivity == project.Activity.IDActivity);
            project1.ProjectState = ProjectStates.First(c => c.IDProjectState == project.ProjectState.IDProjectState);
            project1.StrategicPlanner = Users.First(c => c.IDUser == project.StrategicPlanner.IDUser);
            project1.StartDate = project.StartDate;
            project1.Name = project.Name;
            project1.Brand = brand;
            project1.ProjectTypes = _entities.ProjectTypes.First();
            estimate.Projects.Add(project1);
        }

        _entities.EstimateSet.AddObject(estimate);

        return toCreate;
    }

But this means recreating the father entity, copying over the values, then recreating every child entity, copying the values, assigning it to the father entity, etc. That’s a pain, and I don’t want to write verbose code like this.

ORM should ease me of the pain of writing tons of code for CRUD operations but it seems like this is not the case.

Anyone can help 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-05-18T19:50:01+00:00Added an answer on May 18, 2026 at 7:50 pm

    I had the exact same problem, and had the exact same pains when trying to use EF in a disconnected scenario. And I solved it the same way as you did: reload the object, and ‘replay’ the changes on the domain object (you have to apply a trick to make optimistic concurrency work though). This was the only way I found to make it work.

    Another way to make this work is to send the original entity together with the modified entity. Then you don’t have to reload it, but just attach the original entity and replay the changes. But I’m not sure about the overhead if you send all data twice, don’t really like that.

    Any other way, just fails, crashes, because it seems to mix up contexts, even if they are disposed already.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have some data like this: 1 2 3 4 5 9 2 6
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.