I’m defining POCO’s, used with the Code First capabilities of Entity Framework 4.0.
Part of my data comes from an external source, and for that data I need to keep the identity column value identical to that of the external source, I sometimes need to set the ID myself. But for the same entities I sometimes don’t have an Id and would like an Auto increment like behavior.
Is it possible to define the POCO in such a way, that when I set the value it would use it, and when I don’t, it would auto increment?
According to this mySQL forum answer mySQL is supposed to handle this scenario pretty well, but it seems that with the Entity Framwork doesn’t…
Following on from Lari’s answer, I would suggest writing a stored procedure which sets identity_insert on, inserts records and then off afterwards.
I’d then import that as a function import and then using that inside perhaps an extension method on the entity type itself, or just as a function on the object context.
That way you can a standard:
Type thing, when you want to use the auto incrementing number, and then use something like:
That way you could conditionally use one method or the other. It’s not automatic, but would enable you to use either.