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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:18:05+00:00 2026-06-11T05:18:05+00:00

Hey i am trying to work with entity framework using in DB first mode.

  • 0

Hey i am trying to work with entity framework using in DB first mode.
I have a hierarchy of Enitties that are stored in one table called “DomainEntities”.

When i generated a schema using the entity framework i got the correct mappongs that look like this.

DomainEntity Schema EDMX file.

Now i wonder what is the way i should perform insert using this model.
if i want to insert a new entry with a certain parent do i need to do all this:

[HttpPost]
public ActionResult Create(DomainEntity i_EntityToCreate, int ParentEntityID)
{
    using (var db = new CamelotShiftManagementEntities())
    {
        var parentEntity = db.DomainEntities.Find(ParentEntityID);
        i_EntityToCreate.ParentEntity = parentEntity;
        i_EntityToCreate.EntityTypeID = 1;

        db.DomainEntities.Add(i_EntityToCreate);

        db.SaveChanges();
    }
    return RedirectToAction("Index");
}

is this correct or is there another design i should follow to represent a hierarchy of entities using entity framework ?

  • 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-11T05:18:06+00:00Added an answer on June 11, 2026 at 5:18 am

    There is no need to find the ParentEntity you can use the ParentEntityID directly with DomainEntity object like this, To get the foreign key column in the model, tick the option of “Include foreign key columns in the model” when importing tables in to EDMX.

    After that use below code :

    [HttpPost]
    public ActionResult Create(DomainEntity i_EntityToCreate, int? ParentEntityID)
    {
      using (var db = new CamelotShiftManagementEntities())
      {
        i_EntityToCreate.ParentEntityId = ParentEntityID;
        i_EntityToCreate.EntityTypeID = 1;
    
        db.DomainEntities.Add(i_EntityToCreate);
    
        db.SaveChanges();
      }
    
      return RedirectToAction("Index");
    
    }
    

    If the ParentEntityID is not null than remove the ? mark so that it become not nullable type.

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

Sidebar

Related Questions

Hey I have a really annoying IE7 bug that I am trying to work
Hey, have been trying to work this out for last day or so but
Hey all, I'm trying to work on a simple script that takes a string
Hey I have been trying to work on this for some time but I
Hey all trying to play with nodejs MVC using express and mongoose. I have
Hey all, here is the issue I am currently trying to work through. We
Hey guys i am trying to automate my system at work for sending artwork
Hey there, I am trying to get my signout button to work. This is
Hey, im trying to get an INSERT command to work, but I keep getting
Hey I'm trying to figure out how to convert a statement that works in

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.