简体   繁体   English

Entity Framework Core 不会 map 到 BigInteger

[英]Entity Framework Core will not map to BigInteger

I am needing to use the BigInteger class to handle large integers to my classes, however when trying to use EntityFramework Core to map to a DB table I recieve the following error:我需要使用BigInteger class 来处理我的类的大整数,但是当尝试使用 EntityFramework Core 到 map 到数据库表时,我收到以下错误:

The property AllianceRank.Reputation could not be mapped, because it is of type BigInteger which is not a supported primitive type or a valid entity type.无法映射属性AllianceRank.Reputation ,因为它是BigInteger类型,它不是受支持的原始类型或有效的实体类型。 Either explicitly map this property, or ignore it.要么明确 map 这个属性,要么忽略它。

[Column("reputation")]
public BigInteger Reputation { get; set; }

It would seem that BigIntegers are not a supported type for mapping.似乎BigIntegers不是支持的映射类型。 How can I force it to map, or otherwise resolve this issue?如何将其强制为 map,或以其他方式解决此问题?

Entity Framework would have to make assumptions about how to store that. 实体框架必须对如何存储它做出假设。 If you're using SQL server, for example, bigint doesn't work because your values could be larger or smaller than what's possible with bigint . 例如,如果您正在使用SQL服务器,则bigint不起作用,因为您的值可能大于或小于bigint可能的值。 Would varchar be more appropriate? varchar会更合适吗? Possibly, but if you truly mean it to be a number and not say, an identifier, asking EF to query that as a number is going to be problematic. 可能,但如果你真的认为它是一个数字,而不是说,一个标识符,要求EF查询这个数字将是有问题的。 So essentially, there are less ambiguous types like long or string where you're not leaving the guesswork on how to store it up to Entity Framework. 从本质上讲,有一些不太模糊的类型,如longstring ,你不会忘记如何将它存储到Entity Framework。

Only scalar types, string, and byte[] are supported. 仅支持标量类型,字符串和byte []。 The largest scalar type available is decimal. 可用的最大标量类型是十进制。 Which "only" allows 28-29 significant digits. 哪个“仅”允许28-29位有效数字。

Since EF Core 2.1, you can define Value Conversions when you define data models in the OnModelCreating() function of your DbContext class. 从EF Core 2.1开始,您可以在DbContext类的OnModelCreating()函数中定义数据模型时定义值转换

The best part is that this allows property values to be converted automatically when reading from or writing to the database! 最好的部分是,这允许在读取或写入数据库时自动转换属性值!

So, in your case, you have a BigInteger property ( Reputation ) in C# and a corresponding INT(25) field in MySQL, you can do this: 因此,在您的情况下,您在C#中有一个BigInteger属性( Reputation ),在MySQL中有一个相应的INT(25)字段,您可以这样做:

var converter = new ValueConverter<BigInteger, long>(    
    model => (long)model,
    provider => new BigInteger(provider));

modelBuilder
    .Entity<AllianceRank>()
    .Property(e => e.Reputation)
    .HasConversion(converter);

PS You might actually wanna use a variable-length string type (like VARBINARY) to store the value in MySQL instead of INT(25) , as a C# BigInteger store a much larger value and therefore cause exceptions while converting to INT(25) or C# long . PS您可能实际上想要使用可变长度string类型(如VARBINARY)将值存储在MySQL而不是INT(25) ,因为C# BigInteger存储更大的值,因此在转换为INT(25)或C# long

Use

public Int64 FieldName { get; set; }

In your Model class, it will be "bigint" datatype in SQL在您的模型类中,它将是 SQL 中的“bigint”数据类型

For the poor souls who receive this cryptic error message in a similar situation:对于在类似情况下收到此神秘错误消息的可怜的灵魂:

Unable to cast object of type 'System.Int64' to type 'System.Decimal'.无法将“System.Int64”类型的 object 转换为“System.Decimal”类型。

You would assume that Entity Framework would automatically map a SQL Server bigint (64 bit unsigned integer) to the appropriate C# long (also 64 bit unsigned integer), but noo...您会假设实体框架会自动将 map 和 SQL 服务器 bigint(64 位无符号整数)转换为相应的 C# 长整数(也是 64 位无符号整数)...

So, you have to provide a bit of guidance, in the OnModelCreating(ModelBuilder modelbuilder) method of your DbContext, using HasColumnType() , like so:因此,您必须在 DbContext 的 OnModelCreating(ModelBuilder modelbuilder) 方法中使用HasColumnType()提供一些指导,如下所示:

using Microsoft.EntityFrameworkCore;

namespace Some.Namespace
{
    public class YourClassDbContext : DbContext
    {
        public YourClassDbContext(DbContextOptions<YourClassDbContext> options) : base(options) 
        {
        }

        public YourClassDbContext() {}

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            /* omitted stuff for PK and index */
            modelBuilder.Entity<ClassXYZ>().Property(t => t.Id).HasColumnType("long");
            
            base.OnModelCreating(modelBuilder);
        }
    

With many thanks to @Kelvin Lai above, whose answer put me on the right path, and https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions非常感谢上面的@Kelvin Lai,他的回答让我走上了正确的道路,以及https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions

my problem solved by: by use int64 instead of biginteger我的问题通过以下方式解决:使用 int64 而不是 biginteger

public class MyClassName
    {
        [Key]
        public Int64 TableId { get; set; }
    }

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

相关问题 没有方法将 map 实体添加到实体框架核心中 - No method to map entity to table in entity framework core 有没有一种方法可以在Entity Framework Core中映射临时表? - Is there a way to map a temp table in Entity Framework Core? 实体框架核心 map 相关实体仅当它不是 null - Entity Framework Core map related entity only if it's not null Entity Framework Core:如何将不同的对象实例映射到同一个实体? - Entity Framework Core: how to map different object instances to the same entity? 实体框架核心-如何正确使用复合键映射关系? - Entity Framework Core - How to correctly map relationships with composite key? 使用 AutoMapper 无法 Map 复杂实体框架核心 - Unable to Map Complex Entity Framework Core using AutoMapper 在实体框架核心中执行存储过程而不期望映射到 dbset - execute stored procedure in entity Framework Core without expecting map to dbset 将鉴别器列映射到类 Property Entity Framework Core 2.2.0 - Map Discriminator Column to class Property Entity Framework Core 2.2.0 如何在实体框架核心中将属性映射到类实例? - How can I map a property to a class instance in entity framework core? 网络核心:Map 两个 Class 成员到实体框架中的一列 - Net Core: Map Two Class Members to One Column in Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM