Enity Framework overriding identity column

前端 未结 1 1739
孤城傲影
孤城傲影 2021-01-20 02:38

I have a Code First EF class like so:

public class Event
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { g         


        
相关标签:
1条回答
  • 2021-01-20 03:21

    To start, I am not aware of a good solution for this. I recommend you rethink what you are trying to do, or explain further why you need to do this. There's probably a better solution. The only solution I know of for what you want is ugly, and I can't really recommend it.

    1. You cannot use an identity (auto-increment) field. If you put this on your column, then there is just nothing you can do. Your database will either throw an error, or ignore values you give it.
    2. You will have to handle the auto-increment yourself. There are many ways to do this (store the current value in the database, perform a max() query, etc).
    3. You'll probably be tempted to override SaveChanges(). Here is an SO POST about that. I really do not recommend you do this either.

    Perhaps someone will come along with a really awesome solution for you. Sorry, I know this was more of a non-answer for you. Good Luck.

    0 讨论(0)
提交回复
热议问题