简体   繁体   English

如何使用EF5包括从一种数据库类型到另一种实体类型的转换?

[英]How can I include a conversion from one database type to another entity type with EF5?

The use case is the following: I have a SQLite database and a SQL Server database that are identical. 用例如下:我有一个相同的SQLite数据库和一个SQL Server数据库。 Some users of the product will use the SQLite version, some the SQL Server version. 该产品的某些用户将使用SQLite版本,某些SQL Server版本。 However, by default, the primary key of SQLite, regardless of your CREATE TABLE statement, will internally be stored as a 64 bit integer and the default for our SQL Server is a 32 bit and sometimes a 16 bit integer. 但是,默认情况下,无论您使用CREATE TABLE语句如何,SQLite的主键都将在内部存储为64位整数,而我们的SQL Server的默认值为32位,有时为16位整数。

Because we are talking about large amounts of records, the space needed to store the tables and indexes changes dramatically if we match the datatypes to be 64 bit in both types of databases. 因为我们在谈论大量的记录,所以如果我们将两种数据库中的数据类型匹配为64位,则存储表和索引所需的空间将发生巨大变化。

I would like to use a single mapping with Entity Framework, if at all possible, for the entity types. 如果可能的话,我想对实体类型使用单个映射。 This works for most fields, but not for the primary key fields. 这适用于大多数字段,但不适用于主键字段。 Even if the mapping maps to int , and the field in SQLite is an INT , as soon as I try to load the data, I will receive: 即使映射映射到int ,并且SQLite中的字段是INT ,当我尝试加载数据时,我也会收到:

InvalidOperationException: The type of the key field 'CustomerId' is expected to be System.Int32 but the value provided is actually of type 'System.Int64' InvalidOperationException:关键字段'CustomerId'的类型应为System.Int32,但提供的值实际上是'System.Int64'类型的

I understand the reason of the exception: the SQLite driver returns an Int64 . 我了解例外的原因:SQLite驱动程序返回一个Int64 I do not get this error with other fields in the tables (ie, other DDL declarations of INT map to Int32 and do not throw this error). 我在表中的其他字段中没有收到此错误(即INT其他DDL声明映射到Int32并且不会引发此错误)。

I was wondering if there is an extension point in the Entity Framework, or another solution, some place where I can simply say: "just cast it to an Int32 , it is safe to do so". 我想知道实体框架或其他解决方案中是否有扩展点,在某个地方我可以简单地说:“只要将其转换为Int32 ,这样做是安全的”。

Current ideas involve creating different libraries and dynamic mapping, or some creative generics, but none of the ideas we've had seem satisfiable. 当前的想法涉及创建不同的库和动态映射,或一些创造性的泛型,但是我们似乎没有一个想法可以令人满意。 Any thoughts? 有什么想法吗?

Answering Matthew's question, table are defined with the following pattern: 在回答Matthew的问题时,表的定义如下:

-- works
CREATE TABLE [Customer] (
  [CustomerId] INTEGER NOT NULL PRIMARY KEY ON CONFLICT FAIL, 
  [Name] CHAR NOT NULL ON CONFLICT FAIL);

-- works not (throws above error, unless I manually change 
-- type in EDMX to from int to integer and Int32 to Int64)
CREATE TABLE [Customer] (
  [CustomerId] INT NOT NULL PRIMARY KEY ON CONFLICT FAIL, 
  [Name] CHAR NOT NULL ON CONFLICT FAIL);

This is an old question, but I had a similar problem and here's what I did. 这是一个老问题,但是我有一个类似的问题,这就是我所做的。

I am working with the same entities in both sql server and sqlite using EF6. 我正在使用EF6在SQL Server和sqlite中使用相同的实体。 I was getting the mentioned error when querying using sqlite 使用sqlite查询时出现提示错误

cntx.Set<T>().FirstOrDefault(a => a.ActivityDescription == activityDescription); .

My key was created like [VSDebugBreakModeEnterEventArgsId] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT in my sqlite db. 我的密钥是在我的sqlite数据库中创建的,类似于[VSDebugBreakModeEnterEventArgsId] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT

I was able to change the key on the model to Int64 instead of int and that fixed the problem. 我能够将模型上的键更改为Int64而不是int ,从而解决了该问题。 I didnt have to change anything in my sql server db schema to accommodate this. 我不必更改我的sql server db架构中的任何内容即可解决此问题。 The entity can be inserted, updated, deleted, and queried using an ef context backed by either sqlite or sql server databases. 可以使用由sqlite或sql server数据库支持的ef上下文来插入,更新,删除和查询实体。

Also, this behavior only occurred after I upgraded my ef nugets to EF 6.1.1 from ???. 另外,仅当我将ef nugets从???升级到EF 6.1.1后,才会发生此行为。 At one point there were 3 different projects using 3 different versions of ef and I decided to upgrade them all to the same version. 曾经有3个不同的项目使用ef的3个不同版本,我决定将它们全部升级到相同的版本。 Thats when the above exception started happening. 那就是上述异常开始发生的时候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM