c# - Entity Framework: Autoincrement ID with second Key part -
i have code first ef model class this:
public class contententry { [key, column(order = 0)] public int id { get; set; } [key, column(order = 1)] public string language { get; set; } }
is possible auto increment id
if 0?
for example: want add dataset
id = 1, language = "en"
it works fine. then, want add following:
id = 1, language = "de"
it works fine too. yet, want add one:
id = 0, language = "en"
id
0, db should generate id
me:
id = 2, language = "en"
is there no way, know can generate id
self. happen if 2 threads try add element same id? can prevent this?
class
[table("test")] class test { [databasegenerated(databasegeneratedoption.identity)] [key, column(order = 0)] public int id { get; set; } [key, column(order = 1)] public string language { get; set; } }
insert rec
using (var db = new testdbcontext()) { db.test.add(new test { id = 0, language = "en" }); db.test.add(new test { id = 0, language = "sb" }); db.savechanges(); }
result
id language
1 en
2 sb
3 en
4 sb
rrerence: entity framework: how generate auto-increment id field
Comments
Post a Comment