简体   繁体   English

在Entity Framework 6中为char(16)的long(或整数)

[英]Long (or integer) to char(16) in Entity Framework 6

I need to map a column in a database table that has type char(16) to a field property in an entity (called Number) which is of type Long. 我需要将类型为char(16)的数据库表中的列映射到类型为Long的实体(称为Number)中的字段属性。

Am getting 越来越

Member Mapping specified is not valid. 指定的成员映射无效。 The type 'Edm.Int64 of member 'Number' in 'MyEntity' is not compatible with SqlServer.char. “ MyEntity”中成员“ Number”的类型“ Edm.Int64”与SqlServer.char不兼容。

Is there a way to fix this? 有没有办法解决这个问题?

I have googled but not seeing any clear answer, from what I have read so far EF struggles with the char type but surely there is a way around this? 到目前为止,我已经从EF的char类型中挣扎,但是我一直在谷歌上搜索,但是没有看到明确的答案,但是肯定有办法解决吗?

I'm using EF NUGET package v6.1.3 我正在使用EF NUGET软件包v6.1.3

Mapping a database column of one type to a different type will in your specific case lead to a potential and eventual loss of data. 在您的特定情况下,将一种类型的数据库列映射到另一种类型将导致潜在的甚至最终的数据丢失。

If you store a value that exceeds the maximum allowed value of the short type, which is equivalent to char(16) in SQL, in your long variable in C# you will not be able to store all that data in the database. 如果您存储的值超过了short类型的最大允许值(与SQL中的char(16)相等),则在C#中的long变量中,您将无法在数据库中存储所有这些数据。

Your application and database will contradict each other and you willingly open up your application to bugs and logical challenges. 您的应用程序和数据库将相互矛盾,并且您愿意向错误和逻辑挑战开放您的应用程序。

It is highly recommended that you ensure that your application's variable type is equivalent to the database column type and vice versa because of the aforementioned reasons. 由于上述原因,强烈建议您确保应用程序的变量类型与数据库列类型等效,反之亦然。

The fix you are asking for is ensuring that these data types correspond to each other. 您要求的解决方案是确保这些数据类型相互对应。 There is a reason for this exception and that is simply put that long and short are not compatible with each other in this manner. 出现此异常是有原因的,简而言之,就是长和短不以这种方式相互兼容。 You can store a short value in a long, but you cannot store a long value in a short as that would result in data loss. 您可以将short值存储在long中,但不能将long值存储在short中,因为那样会导致数据丢失。

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

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