none
Enitity Frame work MVC code first remove auto incriment from primairy key RRS feed

  • Question

  • Hi, I have created a table with EF code first but unfortunately I did not add the        

    [DatabaseGenerated(DatabaseGeneratedOption.None)] annotation to it. so a table with this primary key is generated:

    [ComplaintId]               BIGINT         IDENTITY (1, 1) NOT NULL,

    so I try to get get this as a not auto increment field and I added the data annotation [DatabaseGenerated(DatabaseGeneratedOption.None)] to the field and ran the add-migration and Update- database again. but nothing changes.

    somebody know how to do this?

    Thanks.

    Rob


    Rob

    Thursday, March 1, 2018 1:08 PM

All replies

  • What? You don't know how to use a database administration tool like MS SQL Server Management Studio and do this?
    Thursday, March 1, 2018 10:59 PM
  • Hi rob warning,

    >>so I try to get get this as a not auto increment field and I added the data annotation [DatabaseGenerated(DatabaseGeneratedOption.None)] to the field and ran the add-migration and Update- database again. but nothing changes.

    Because we could not use ALTER COLUMN to set whether a column is an identity column. >>somebody know how to do this?

    As DA924x said, we could use related database tools to achieve it, if you want to use entity framework migration to achieve it, we need to drop the table and generate it again.

    Note: please backup your database before you drop the table.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 2, 2018 1:16 AM
    Moderator
  • of course I can do that but this is not code firs approach.

    Rob

    Friday, March 2, 2018 9:36 AM
  • Hi rob warning,

    As I mention before, it is a database issue, we could not execute ALTER COLUMN to remove a column identity property, which is an identity column. so you could not use [DatabaseGenerated(DatabaseGeneratedOption.None)] or Fluent API to achieve it. we need to drop the table first, then re-generate related table if you want to use code first approach.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 2, 2018 9:42 AM
    Moderator
  • of course I can do that but this is not code firs approach.

    Rob

    What does database administration have  to do with code first? When did an ORM take over database administration, which is a crutch? A DBA would cringe at this no doubt. When did a software developer turn into a DBA through ORM code? It's too ridiculous. :) 

    This is just another reason that I will never use code first. :)

    Friday, March 2, 2018 10:06 AM