Cannot insert explicit value for identity column - into related table

entity-framework-core

Question

I have a database first model.

Database Diagram(Partial)

My application UI provides a group of checkboxes, one for each value in Data_Type.

When the user checks one, I expect a row to be added in BUS_APPL_DATA_TYPE, however I'm getting an error about Cannot insert explicit value for identity column in DATA_TYPE (And I absolutely do not actually want to insert data in this table)

My EF Model class for BUS_APPL has this property

public ICollection<BusApplDataType> BusApplDataType { get; set; }

And that EF Model class looks like

public partial class BusApplDataType
{
    public int BusApplId { get; set; }
    public int DataTypeId { get; set; }

    [Newtonsoft.Json.JsonIgnore]
    public BusAppl BusAppl { get; set; }
    public DataType DataType { get; set; }
}

What exactly do I need to add to the BusApplDataType collection to get a record to be inserted in BUS_APPL_DATA_TYPE?

Edit: At a breakpoint right before SaveChanges. The item at index 2 is an existing one and causes no issues. The item at index 3 is new. Without this everything updates fine. There is a DATA_TYPE with id 5 in the database.

enter image description here

The surrounding code, if it helps.

    [HttpPut("{id}")]
    public IActionResult Update(int id, [FromBody] BusAppl item)
    {
       ...
       var existing = _context.BusAppl.FirstOrDefault(t => t.Id == id);
       ...
       existing.BusApplDataType = item.BusApplDataType; //A bunch of lines like this, only this one causes any issue.
       ...
       _context.BusAppl.Update(existing);
       _context.SaveChanges();
       return new NoContentResult();
    }
1
0
4/13/2018 5:12:05 PM

Accepted Answer

My issue was that I needed to use my context to look up the actual entity, using info passed, instead of using the one with all the same values that was passed into my api directly.

0
4/17/2018 12:49:04 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow