【发布时间】:2012-03-25 10:22:56
【问题描述】:
我正在尝试将当前使用自定义 DAO 框架的项目转换为使用实体框架。该系统非常大,因此对数据库(如果重要的话是 SQL Azure DB)本身的更改并不是特别可行,应尽可能避免。
问题在于 ID 列。不幸的是,在创建系统时,有些表的数据类型为 bigint,而有些表的数据类型为 int - 但模型本身都来自 ID 为 long 的基类。以前的框架能够处理这种情况,但我一直无法找到使用实体框架的方法。
下面是我能想到的最简单的例子:
public class Context : DbContext {
public IDbSet<Foo> Foos {get;set;}
public IDbSet<Bar> Bars {get;set;}
}
public abstract class BaseClass {
public long ID;
}
public class Foo : BaseClass {
...
}
public class Bar : BaseClass {
...
}
SQL Table: Foo
+-------------+
| id : bigint |
| ... |
+-------------+
SQL Table : Bar
+-------------+
| id : int |
| ... |
+-------------+
当我尝试加载 Bar 模型时,我收到此错误:
The 'ID' property on 'BaseClass' could not be set to a 'Int32' value. You must set this property to a non-null value of type 'Int64'.
我想找到一种方法告诉系统 Bar 有整数,而 Foo 有长整数。我尝试在上下文中覆盖OnModelCreating 并为Bar 定义HasColumnType。这给了我一个新的错误:
Schema specified is not valid. Errors:
(105,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.Int64[Nullable=False,DefaultValue=]' of member 'ID' in type 'Bar' is not compatible with 'SqlServer.int[Nullable=False,DefaultValue=,StoreGeneratedPattern=Identity]' of member 'ID' in type 'CodeFirstDatabaseSchema.Bar'.
在我看来,如果在将请求发送到服务器之前,我只能将 BaseClass 的 ID 的预期数据类型更改为 int,那么我应该能够向上转换为 @收到回复后987654334@。理想情况下,我希望在每个班级的基础上执行此操作。
谁能指出我正确的方向?
【问题讨论】:
标签: c# entity-framework data-binding azure-sql-database sqldatatypes