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:
- In SSMS generate scripts (schemat only)
- Delete database
- Create database the same name on old db
- fix my migration class (add identity:true where where missing)
- 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'