Entity Framework Code First - pass smallint and integer to int32

I am working on a small database service tool. My problem is that due to the last update, some small outlines had to be changed to integer ones.

public class TEST { public int ID { get; set; } //public Int16 ID { get; set; } public string TEST { get; set; } } 

I changed the type from Int16 to int. Everything works fine, except that I can no longer use it with the old version of the database. The exception is something like "Expected System.Int32 found by Typ System.Int16."

Is there a way to pass all smallint and integer to int32?

Any ideas? My environment: EntityFramework 5.0.0.NET 4.5 FirebirdClient 3.0.2.0

I tried to force a throw in buildbuilder:

  modelBuilder.Entity<TEST>() .Property(p => p.ID) .HasColumnType("smallint"); 

An exception:

error 2019: Membership indication is not valid. The type 'Edm.Int32 [Nullable = False, DefaultValue =]' of the participant identifier 'in Typ' ContextRepository.TEST 'is not compatible with the' FirebirdClient.smallint [Nullable = False, DefaultValue =, StoreGeneratedPattern = Identity] 'of the member "SCHLUESSEL" in type " CodeFirstDatabaseSchema.BUNDLAND "

Create an Int16 identifier and then run everything in smallint (HasColumnType ("int")) works fine, but will give me exceptions with numbers greater than 31767 (small maximum) ...

+6
source share
1 answer

I found a solution for my problem! I need to use Int16 in my model and use the model constructor to set the colum type to smallint:

 public class TEST { public Int16 ID { get; set; } } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<TEST>().HasKey(a => new { a.ID}); modelBuilder.Entity<TEST>() .Property(p => p.ID) .HasColumnType("SMALLINT"); base.OnModelCreating(modelBuilder); } 

Now I can pass the int property without exception (even with numbers> 32767):

 var lQry = (from b in ctData.TEST select new { ID = (int)b.ID, }); 
+8
source

Source: https://habr.com/ru/post/951227/


All Articles