0

I create class and add migration. All class with base properties look like:

  public class CustomersInquiries : BaseModel
    {
        [Key]
        public int CustomersInquiryId { get; set; }
    }

BaseModel - is my custom field (addedBy,addedDate etc)

Some time ago , I add many tables for my database. Migration code which is generate create all table witch identity NO, and the keys are not automatically increments.

I tried create new migration witch change on my model or OnModelCreating (no mixed):

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

or

  modelBuilder.Entity<CustomersInquiries>()
   .Property(c => c.CustomersInquiryId)
   .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

with no effect. Migration code generates empty.

How to fix my database?

EDIT:

I do:

  1. In SSMS generate scripts (schemat only)
  2. Delete database
  3. Create database the same name on old db
  4. fix my migration class (add identity:true where where missing)
  5. in pmc update-database -verbose -force

This steps solved my problem but the next time something like this I will be able to waste time searching for repro. I do not know why bad generate these tables.

EDIT2:

to validate wrote the script. It may be useful to someone.

SELECT i.name AS IndexName, OBJECT_NAME(ic.OBJECT_ID) AS TableName, 
       COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName,

    ident.seed_value, ident.increment_value, ident.last_value
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id
LEFT OUTER JOIN sys.identity_columns AS ident
ON i.object_id = ident.object_id
WHERE i.is_primary_key = 1 AND OBJECT_NAME(ic.OBJECT_ID) <> N'__MigrationHistory'
18666
  • 125
  • 1
  • 18
  • See this http://stackoverflow.com/questions/1049210/adding-an-identity-to-an-existing-column – Steve Greene Feb 05 '17 at 17:04
  • I know how to add to an existing table. But I do not like that in the code first I mark column attribute [Key] and identity column not generated in db. – 18666 Feb 05 '17 at 17:08
  • I thought your question was about changing existing columns to identity - migrations won't pick that up. You need to use the technique in the link. Not sure why they were not added as identity originally, because the default convention for integer keys is to use identity. – Steve Greene Feb 05 '17 at 17:27

0 Answers0