[MSSQL] growth since the id field, you can not insert data

IDENTITY_INSERT is OFF, data can not be inserted,

Similar mistakes, solve record:

 

Online search the next, all solutions under the First Code mode,

The: OnModelCreating inside method DBContext

// EntityFramework .NET Core
modelBuilder.Entity<Project>().Property(t => t.Id).ValueGeneratedOnAdd();

//...

// EntityFramework
modelBuilder.Entity<Project>().Property(t => t.Id)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

 

Or: In the class definition entities to id's annotated as follows

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

 


 

The above method, without using various modes, not working,

Finally found that as long as the front end to the back end when the pass json, they do not pass the id field on it,

However, residues hand to pass to the default value of -1. . .

In fact, there never was a problem (back-end interview with class object parameters are received, id default is 0, then the Add method properly and to value other than 0 will be error, unable to auto-increment assignment)

 

Guess you like

Origin www.cnblogs.com/CoderMonkie/p/entityframework-identity-insert.html