locked
Foreign key issue in entity framework core Creation method RRS feed

  • Question

  • User-183185495 posted

    I have a case object and a case can have many relationships. They are poi's and vessels. however when I come to the poi create I need to no the case id and the realtion ship Id. I have been using session but when Im creating the object it wont no the relationship so how do I avoid this error do 

    The case has already been created and they click a button to get the to the create like so

    I need some way of passing the case Id which would be Model.Id  here to the Poi Create function so that i can avoid this error.

    Please see small screen cast of issue

    https://www.screencast.com/t/dljtknx0t

    Here is the error I am getting. I am using asp.net core 3.1 and EF Core 3.1

    DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
    SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Pois_RelationShips_RelationShipId". The conflict occurred in database "MISSystem", table "dbo.RelationShips", column 'Id'. The statement has been terminated.

    <div style="margin-top:10px;">
    <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true"
    aria-expanded="false">
    Add Relationship
    </a>
    <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
    <a class="dropdown-item" href="/Poi/Create">Person</a>
    <a class="dropdown-item" href="/Vessels/Create">Vessel</a>
    <a class="dropdown-item" href="#">Location</a>
    </div>
    </li>
    </div>



    // POST: POI/Create
    // To protect from overposting attacks, enable the specific properties you want to bind to, for 
    // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,FirstName,MiddleName,LastName,Nationallity,DOB,Age,Alias,PlaceOfBirth,CountryOfBirth,Deceased,DeceasedDate,AddressId,EmailAddress,FacebookUrl,TwitterUrl,LinkedInUrl,CopmanyName,CompanyAddress,StartDate,EndDate,isDeleted,isActive,CreatedDate,CreatedBy")] POI pOI) {
            var caseId = Int32.TryParse(HttpContext.Session.GetString("CaseId"), out int resultcaseId);
            var realtionShipId = Int32.TryParse(HttpContext.Session.GetString("relationShipId"), out int resultRelationshipId);
    
            if (ModelState.IsValid)
            {
                    pOI.MISObjectId = resultcaseId;
                pOI.RelationShipId = resultRelationshipId;
                pOI.isActive = true;
                pOI.isDeleted = false;
    
                _context.Add(pOI);
                await _context.SaveChangesAsync();
    
                var validId=HttpContext.Session.GetString("relationShipId");
    
                return RedirectToAction("Edit", "Relationships", new { Id = validId });
    
                 
            }
    
            return View(pOI);
    }
    

    Tuesday, July 14, 2020 5:01 PM

All replies

  • User-217098811 posted

    Hi roguenidb

    The reason why this error is realistic is that you added RelationShipId in the Pois_RelationShips table

    but the ID does not exist in your RelationShips table, which will cause foreign key constraint conflicts.

    If you want to add RelationShipId, you should get your RelationShips table ID in. How do you store your data through Session?

    Best Regards

    yinqiu

    Wednesday, July 15, 2020 5:32 AM