public class TEST { public int ID { get; set; } //public Int16 ID { get; set; } public string TEST { get; set; } }
我将类型从Int16更改为int.一切正常,除了我不能再使用旧版本的数据库了. Exception类似于“System.Int32 expect,found Typ System.Int16”.@H_403_5@
有没有办法将所有smallint和整数强制转换为int32?@H_403_5@
有任何想法吗?我的环境:EntityFramework 5.0.0 .NET 4.5 FirebirdClient 3.0.2.0@H_403_5@
我试图在模型构建器中强制执行:@H_403_5@
modelBuilder.Entity<TEST>() .Property(p => p.ID) .HasColumnType("smallint");
例外:@H_403_5@
error 2019: Member Mapping specified is not valid. The type ‘Edm.Int32[Nullable=False,DefaultValue=]’ of member ‘ID’ in Typ ‘ContextRepository.TEST’ is not compatible with ‘FirebirdClient.smallint[Nullable=False,DefaultValue=,StoreGeneratedPattern=Identity]’ of member ‘SCHLUESSEL’ in type ‘CodeFirstDatabaseSchema.BUNDLAND’@H_403_5@
使ID Int16然后将所有内容转换为smallint(HasColumnType(“int”))工作正常但会给出异常数字大于31767(最小smallint)…@H_403_5@ @H_301_22@
解决方法
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); }
现在我可以将属性转换为int而没有异常(即使数字> gt; 32767):@H_403_5@
var lQry = (from b in ctData.TEST select new { ID = (int)b.ID,});@H_301_22@ @H_301_22@ 原文链接:https://www.f2er.com/csharp/99884.html