0

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();
    }
Chris
  • 679
  • 1
  • 11
  • 26
  • possible duplicate: https://stackoverflow.com/questions/11173562/entity-framework-error-cannot-insert-explicit-value-for-identity-column-in-tabl – Ryan Wilson Apr 13 '18 at 15:09
  • Thanks, I don't think it's the same thing though, as I believe that guy is trying to insert into the table it's complaining about. I'm using the framework incorrectly somehow and it's resulting in an error about inserting into a table I didn't intend to insert anything into in the first place. – Chris Apr 13 '18 at 15:14
  • true, it may not be exactly the same, but I believe the error is generated in the same way. It gives some guidance on what can cause that specific error and how to fix it. – Ryan Wilson Apr 13 '18 at 15:16
  • Please post the code that generates the exception. Most likely you are not attaching the objects correctly and EF thinks that the `DataType` object is new. – Ivan Stoev Apr 13 '18 at 15:23
  • I added an image of what the data object contains immediately before SaveChanges is called. – Chris Apr 13 '18 at 15:35
  • Screenshots are useless. We cannot help w/o seeing the relevant code (e.g. [mcve]). – Ivan Stoev Apr 13 '18 at 16:56
  • Ok, hopefully someone else can. I think the screenshot here showing what the code is trying to save is more useful than the code which is pretty boilerplate. I really don't even know what to show code wise. This is the object that I am attempting persist with the framework. – Chris Apr 13 '18 at 17:00
  • What I meant was that it's not so important *what* the code is trying to save, but *how* it is trying to do so. Applying disconnected entity graph modifications is not a trivial task. The "boilerplate" code usually works only for simple entities w/o related data. – Ivan Stoev Apr 14 '18 at 11:01
  • I added what seems like the relevant code. – Chris Apr 15 '18 at 13:53

1 Answers1

0

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.

Chris
  • 679
  • 1
  • 11
  • 26