c# - Entity Framework Code First - Cast smallint and integer to int32 -
im working on small service tool database. problem due last update, smallint-colums had be changed integer.
public class test { public int id { get; set; } //public int16 id { get; set; } public string test { get; set; } }
i changed type int16 int. works fine, except can't use old version of database anymore. exception "system.int32 expected, found typ system.int16".
is there way cast smallint , integer int32?
any ideas? environment: entityframework 5.0.0 .net 4.5 firebirdclient 3.0.2.0
i tried force cast in modelbuilder:
modelbuilder.entity<test>() .property(p => p.id) .hascolumntype("smallint");
exception:
error 2019: member mapping specified not valid. type 'edm.int32[nullable=false,defaultvalue=]' of member 'id' in typ 'contextrepository.test' not compatible 'firebirdclient.smallint[nullable=false,defaultvalue=,storegeneratedpattern=identity]' of member 'schluessel' in type 'codefirstdatabaseschema.bundland'
make id int16 , casting smallint (hascolumntype("int")) works fine give me exceptions numbers bigger 31767(smallint max)...
i found solution problem! have use int16 in model , use modelbuilder set colum-type 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 can cast property int without exception (even numbers > 32767):
var lqry = (from b in ctdata.test select new { id = (int)b.id, });
Comments
Post a Comment