Error in CodeFirst Seed with Migrations: Modifying a column with an Identity pattern is not supported. Column: "CreatedAt".

I have activated migration in my Azure Mobile Services project. I filled in a new seed function. Inside the class Configuration.cs migration. If the tables are empty, the seed function goes without problems. When my AddorUpdate tries to update the first object, I get an error in the internal exception: "Changing the column with the" Identification "template is not supported. Column:" CreatedAt ". Table:" CodeFirstDatabaseSchema.Category ".

Part of my code is as follows:

context.categories.AddOrUpdate(
            new Category { Id="1", Code="GEN", Text="General"},
            new Category { Id="2", Code="POL", Text="Politics"},
            new Category { Id="3", Code="FAS", Text="Fashion"},
            new Category { Id="4", Code="PEO", Text="People"},
            new Category { Id="5", Code="TEC", Text="Technology"},
            new Category { Id="6", Code="SPO", Text="Sport"},
            new Category { Id="7", Code="LIV", Text="Living"}
        );
+4
source share
4

.

: CreateAt , :

    private void AddOrUpdatePreservingCreatedAt<T> (DbSet<T> set, T item) where T : EntityData
    {        
        var existing = set.Where(i => i.Id == item.Id).FirstOrDefault();
        if (existing != null)
        {
            item.CreatedAt = existing.CreatedAt;             
        }
        set.AddOrUpdate(i => i.Id, item);
    }

:

AddOrUpdatePreservingCreatedAt(context.YourItems, itemToBeUpdatedOrAdded);
+4

, .

AddOrUpdate. : http://thedatafarm.com/data-access/take-care-with-ef-4-3-addorupdate-method/

, , , AddOrUpdate.

, , , , null CreatedAt. EntityData, CreateAt :

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] //Here they mark this as IDENTITY        
[Index(IsClustered = true)] // Cluster index. i really dont know why ?         
[TableColumn(TableColumnType.CreatedAt)]         
public DateTimeOffset? CreatedAt { get; set; }

, , CreatedAt.

, , , CreateAt , addOrUpdate:

 // Create List       
 List<Permission> permissions = new List<Permission>(new Permission[]{             
   new Permission { Id = "ID1" , Name = "Send SMS"},           
   new Permission { Id = "ID2", Name = "Send Email"}            });
   // Iterate through list to set CreatedAt to correct value 
   foreach (Permission p in permissions){
      // Get the record from the db if it exists              
      var t = context.PermissionSet.Where(s => s.Id == p.Id).FirstOrDefault();
      if (t != null){
         p.CreatedAt = t.CreatedAt; //SET CreatedAt to correct Value if the record already exists                
      }
      context.PermissionSet.AddOrUpdate(a => a.Id, p); // NOW I CAN UPDATE WITH NO PROBLEM
   } 

, .:)

+3

Id, Entity Framework , , IDENTITY .

Id IDENTITY, , Id = 1, Id = 2 ..

, , , "CreatedAt". , DateTime , , , IDENTITY?

, , , , , EF , . , CODE , Seed :

context.categories.AddOrUpdate(
   x => x.Code,//the natural key is Code    
   new Category { Code="GEN", Text="General"},
   new Category { Code="POL", Text="Politics"},
   new Category { Code="FAS", Text="Fashion"},
   new Category { Code="PEO", Text="People"},
   new Category { Code="TEC", Text="Technology"},
   new Category { Code="SPO", Text="Sport"},
   new Category { Code="LIV", Text="Living"}

);

: AddOrUpdate

+1

, , .

, , . Identity. - . , .

SO .

MSSQL .

0

All Articles