开发者

Inserting a record with specified primary key into MySQL via Entity Framework

We began using Entity Framework with MySQL in our project recently. Now I am writing unit tests for the dat开发者_运维问答a access level; for that purpose I have created a database with some test data.

In the test-method for the Delete method I want to first delete a specified record and then insert it again with all the fields holding exactly the same values, including the Id column which is set as the primary key. The purpose is to keep the test data in the DB.

But when I insert previously deleted record, Entity Framework just ignores the Id value of the entity and thus a new Id is generated using AUTO_INCREMENT.

Thanks in advance.


AUTO_INCREMENT means that you do not get to set the Id value, period. You just can't do this.

What you should do instead is:

  1. Insert a new row.
  2. SaveChanges()
  3. Now read the Id value on the object, which will have been updated from the database.
  4. Go ahead and delete the row, using this Id value.


Have you used the insert ignore statement?

It helps you forcefully insert pks with specified value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜