Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架代码优先-将smallint和integer强制转换为int32_C#_Entity Framework_Ef Code First_Firebird - Fatal编程技术网

C# 实体框架代码优先-将smallint和integer强制转换为int32

C# 实体框架代码优先-将smallint和integer强制转换为int32,c#,entity-framework,ef-code-first,firebird,C#,Entity Framework,Ef Code First,Firebird,我正在为一个数据库开发一个小的服务工具。我的问题是,由于上次更新,一些smallint列必须更改为整数 public class TEST { public int ID { get; set; } //public Int16 ID { get; set; } public string TEST { get; set; } } 我将类型从Int16更改为int。一切正常,只是我不能再将其用于旧版本的数据库。异常类似于“System.Int32预期,found Ty

我正在为一个数据库开发一个小的服务工具。我的问题是,由于上次更新,一些smallint列必须更改为整数

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

}
我将类型从Int16更改为int。一切正常,只是我不能再将其用于旧版本的数据库。异常类似于“System.Int32预期,found Typ System.Int16”

有没有办法将所有smallint和integer强制转换为int32

有什么想法吗?我的环境:EntityFramework 5.0.0.NET 4.5 FirebirdClient 3.0.2.0

我试图在modelbuilder中强制转换:

        modelBuilder.Entity<TEST>()
        .Property(p => p.ID)
        .HasColumnType("smallint");
modelBuilder.Entity()
.Property(p=>p.ID)
.HasColumnType(“smallint”);
例外情况:

错误:指定的成员映射无效。类型“ContextRepository.TEST”中成员“ID”的类型“Edm.Int32[Nullable=False,DefaultValue=]”与类型“CodeFirstDatabaseSchema.BUNDLAND”中成员“SCHLUESSEL”的“FirebirdClient.smallint[Nullable=False,DefaultValue=,StoreGeneratedPattern=Identity]”不兼容


将ID设置为Int16,然后将所有内容强制转换为smallint(HasColumnType(“int”))效果很好,但会给我带来大于31767(smallint max)的异常。

我找到了解决问题的方法!我必须在模型中使用Int16,并使用modelbuilder将Column类型设置为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);
}

为什么你不能升级数据库,在本专栏中使用INTEGER?我只是在写一个维护程序,担心主应用程序中的副作用…我不明白这是如何编译的?您有两个同名的属性。是否需要HasColumnType(“SMALLINT”)?declare int16还不够吗?@Michaelfreedgeim,我刚刚测试了这个,对我来说,仅仅声明int16就足够了
var lQry = (from b in ctData.TEST
    select new
    {
        ID = (int)b.ID,
    });