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.
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 ?

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 :
If the ParentEntityID is not null than remove the ? mark so that it become not nullable type.