Handling temporary creation of foreign key table values

115 Views Asked by At

I have a foreign table in my context called relationships each case can have many relationships. However I don't want to hit the final save until they have saved their full relationship until they finish there data entry but I still need a reference to it.

I am on here

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,RelationShipType")] RelationShips relationShips)
{
    if (ModelState.IsValid) 
    {
        var caseId = Int32.TryParse(HttpContext.Session.GetString("CaseId"), out int resultcaseId);
        relationShips.RelationShipType = relationShips.RelationShipType;
        relationShips.isAcitve = true;
        relationShips.MisObjectId = resultcaseId;
        _context.Add(relationShips);
         await _context.SaveChangesAsync();
    }

    return RedirectToAction("Edit", "Relationships", new { Id = relationShips.Id });
}

Obv I could just remove the line

await _context.SaveChangesAsync();

and pass in the relationship object as such

return RedirectToAction("Edit", "Relationships", new { Id = relationShips.Id }, res);

But is that the correct way of handling it I have relationship setup as a one to many to my case

modelBuilder.Entity<RelationShips>()
   .HasOne<MISObject>(s => s.Case)
   .WithMany(g => g.RelationShip)
   .HasForeignKey(s => s.MisObjectId);

Or should I be doing.

_context.MSIobject.Relationships.Add 

The only thing is

Even if i have it here in my DbContext I can't seem to access from MSIobject

public DbSet<RelationShips> RelationShips { get; set; }
0

There are 0 best solutions below