关闭ID的自动递增,并用API中的自定义值替换

时间:2019-09-12 14:59:00

标签: c# .net entity-framework

从API中提取数据,并用所述数据填充我的数据库。数据库中的所有字段都按应有的方式填充,但ID字段会自动递增。需要关闭此功能,因为我依靠API的ID来操作我的大部分网站。

围绕Google并尝试使用它

[System.ComponentModel.DataAnnotations.KeyAttribute()]

尽管没做任何事情。...

这是我从API调用中接收数据的方式,所有方法都检出:

[{"id":"40387124","name":"Abyss Flower","type":"Normal Monster","desc":"A rarely seen flower that blossoms quietly on the edge of darkness.","atk":"750","def":"400","level":"2","race":"Plant","attribute":"EARTH","card_images":[{"id":"40387124","image_url":"https://storage.googleapis.com/ygoprodeck.com/pics/40387124.jpg","image_url_small":"https://storage.googleapis.com/ygoprodeck.com/pics_small/40387124.jpg"}],"card_prices":{"cardmarket_price":"0.00","tcgplayer_price":"0.00","ebay_price":"10.99","amazon_price":"0.00"}}]

但是,我的桌子是这样填充的:

19  Abyss Flower    750 400 A rarely seen flower that blossoms quietly on the edge of darkness. 2   Normal Monster  EARTH

我需要能够接受来自API 40387124的ID,而不是19(这是从数据库生成的自动递增的ID)

1 个答案:

答案 0 :(得分:0)

您可以将Key属性与数据库生成的选项一起使用:

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

或者如果您使用的是fluentAPI,则如下所示:

modelBuilder.Entity<MyEntity>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);