Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

web api 406 not acceptable error occur when models changed - dBContext issue

$
0
0

I'm working on 

web api versioning
 code 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 

DbContext
 to 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.cs
 I have manually added new column 
MyNewName
 into 
Customer Table

But for 

Version=2
 I'm getting error : 
406 Not acceptable
 For 
Version=1
 it is working good.

I think something is related with code first, can any one point out and help me?


Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>