简体   繁体   English

如何处理可能为int或long的Entity Framework CodeFirst列

[英]How to handle an Entity Framework CodeFirst Column that could be an int OR a long

We have written an Entity Framework CodeFirst mapping for a number of tables created and managed by a third-party application. 我们已经为由第三方应用程序创建和管理的许多表编写了一个Entity Framework CodeFirst映射。 They've recently upgraded a number of the columns in these tables from SQL data type INT to BIGINT. 他们最近将这些表中的许多列从SQL数据类型INT升级到BIGINT。

The only way I can think of for achieving this is by having two separate database models, one for the old version, one for the new version, but I was wondering if there was a way to treat the field in .net as a long, and then in the case where it attempts to read to or from the old style database format it automagically casts it to/from an int? 我能想到的唯一方法是拥有两个单独的数据库模型,一个用于旧版本,一个用于新版本,但是我想知道是否有一种方法可以将.net中的字段视为长字段,然后,如果尝试读取旧式数据库格式或从旧式数据库格式读取数据,它会自动将其强制转换为int?

Thanks. 谢谢。

Most problems in programming can be solved with another layer of abstraction, this being no exception. 编程中的大多数问题都可以通过另一层抽象来解决,这也不例外。

If you have CREATE VIEW permission in the database, the easiest solution would be to abstract away the table behind a view, casting the column to a BIGINT in all cases. 如果您在数据库中具有CREATE VIEW权限,最简单的解决方案是在视图后面提取表,在所有情况下都将列转换为BIGINT All the application has to be aware of is this view, and your code-first model simply becomes the one with the long . 应用程序只需要了解这种视图,您的代码优先模型就可以轻松地成为long You should still be able to perform all CRUD operations using the view - provided you don't overflow the small size field of course. 您当然仍然可以使用该视图执行所有CRUD操作-前提是您当然不会溢出小尺寸字段。

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

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