简体   繁体   中英

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. They've recently upgraded a number of the columns in these tables from SQL data type INT to 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?

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. All the application has to be aware of is this view, and your code-first model simply becomes the one with the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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