I'm working on
web api versioningcode is from here
Facing issue when version change model class is also changed. I'm not sure but I think error is because of models change (used code first).
in version 1 I used below customer model class
ModelVersion1.Customer.cspublicintId{ get;set;}publicstringTitle{ get;set;}
in version 2 I used below customer class
ModelVersion2.Customer.cspublicintId{ get;set;}publicstringTitle{ get;set;}publicstringMyNewName{ get;set;}
I have created two different
DbContextto use two different model classes.
dbcontextV1.cs
protectedoverridevoidOnModelCreating(DbModelBuilder modelBuilder){Database.SetInitializer<DbContextV1>(null); modelBuilder.Entity<ModelVersion1.Customer>().HasKey(x => x.Id); modelBuilder.Entity<ModelVersion1.Customer>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);}publicvirtualDbSet<ModelVersion1.Customer>Customers{ get;set;}
dbcontextV2.cs
protectedoverridevoidOnModelCreating(DbModelBuilder modelBuilder){Database.SetInitializer<DbContextV2>(null); modelBuilder.Entity<ModelVersion2.Customer>().HasKey(x => x.Id); modelBuilder.Entity<ModelVersion2.Customer>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);}publicvirtualDbSet<ModelVersion2.Customer>Customers{ get;set;}
Now I send a request using fiddler like below-
http://localhost:8090/api/Customers
Header - Accept :application/json;version=1
Database was already created by using
ModelVersion1.Customer.csI have manually added new column
MyNewNameinto
Customer Table
But for
Version=2I'm getting error :
406 Not acceptableFor
Version=1it is working good.
I think something is related with code first, can any one point out and help me?