• <tfoot id='l9zes'></tfoot>

      <legend id='l9zes'><style id='l9zes'><dir id='l9zes'><q id='l9zes'></q></dir></style></legend>
      <i id='l9zes'><tr id='l9zes'><dt id='l9zes'><q id='l9zes'><span id='l9zes'><b id='l9zes'><form id='l9zes'><ins id='l9zes'></ins><ul id='l9zes'></ul><sub id='l9zes'></sub></form><legend id='l9zes'></legend><bdo id='l9zes'><pre id='l9zes'><center id='l9zes'></center></pre></bdo></b><th id='l9zes'></th></span></q></dt></tr></i><div id='l9zes'><tfoot id='l9zes'></tfoot><dl id='l9zes'><fieldset id='l9zes'></fieldset></dl></div>
      • <bdo id='l9zes'></bdo><ul id='l9zes'></ul>
    1. <small id='l9zes'></small><noframes id='l9zes'>

        如何在实体框架中使用 unsigned int/long 类型?

        How to use unsigned int / long types with Entity Framework?(如何在实体框架中使用 unsigned int/long 类型?)

          • <bdo id='5NrWX'></bdo><ul id='5NrWX'></ul>
              <tbody id='5NrWX'></tbody>
              <legend id='5NrWX'><style id='5NrWX'><dir id='5NrWX'><q id='5NrWX'></q></dir></style></legend>

              <small id='5NrWX'></small><noframes id='5NrWX'>

              <tfoot id='5NrWX'></tfoot>
              <i id='5NrWX'><tr id='5NrWX'><dt id='5NrWX'><q id='5NrWX'><span id='5NrWX'><b id='5NrWX'><form id='5NrWX'><ins id='5NrWX'></ins><ul id='5NrWX'></ul><sub id='5NrWX'></sub></form><legend id='5NrWX'></legend><bdo id='5NrWX'><pre id='5NrWX'><center id='5NrWX'></center></pre></bdo></b><th id='5NrWX'></th></span></q></dt></tr></i><div id='5NrWX'><tfoot id='5NrWX'></tfoot><dl id='5NrWX'><fieldset id='5NrWX'></fieldset></dl></div>

                1. 本文介绍了如何在实体框架中使用 unsigned int/long 类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  具有 long 数据类型的类属性在添加新迁移(代码优先)时正确映射,但 ulong 数据类型被 mysql 的 EF 提供程序跳过.如何映射一个属性来使用mysql的unsigned bigint?

                  Class properties with the long data type are properly mapped when adding a new migration (code-first), but ulong data types are skipped by mysql's EF provider. How does one map a property to use mysql's unsigned bigint?

                  推荐答案

                  2021 年 2 月更新

                  显然 EF Core 现在支持 ulong -- 请参阅下面@JimbobTheSailor 的回答.

                  Apparently EF Core now supports ulong -- see @JimbobTheSailor's answer below.

                  较旧的实体框架版本:

                  事实证明实体框架不支持 unsigned 数据类型.对于 uint 列,可以将值存储在具有更大范围的有符号数据类型中(即 long).ulong 列呢?通用解决方案对我不起作用,因为没有 EF 支持的签名数据类型可以保存 ulong 而不会溢出.

                  Turns out that Entity Framework does not support unsigned data types. For uint columns, one could just store the value in a signed data type with a larger range (that is, a long). What about ulong columns? The common solution couldn't work for me because there is no EF-supported signed data type that can hold a ulong without overflowing.

                  经过一番思考,我想出了一个简单的解决方案:只需将数据存储在支持的long类型中,并在访问时将其转换为ulong.您可能会想:等等,ulong 的最大值 >long 的最大值!"您仍然可以将 ulong 的字节存储在 long 中,然后在需要时将其转换回 ulong,因为两者都有 8 个字节.这将允许您通过 EF 将 ulong 变量保存到数据库中.

                  After a bit of thinking, I figured out a simple solution to this problem: just store the data in the supported long type and cast it to ulong when accessed. You might be thinking: "But wait, ulong's max value > long's max value!" You can still store the bytes of a ulong in a long and then cast it back to ulong when you need it, since both have 8 bytes. This will allow you to save a ulong variable to a database through EF.

                  // Avoid modifying the following directly.
                  // Used as a database column only.
                  public long __MyVariable { get; set; }
                  
                  // Access/modify this variable instead.
                  // Tell EF not to map this field to a Db table
                  [NotMapped]
                  public ulong MyVariable
                  {
                      get
                      {
                          unchecked
                          {
                              return (ulong)__MyVariable;
                          }
                      }
                  
                      set
                      {
                          unchecked
                          {
                              __MyVariable = (long)value;
                          }
                      }
                  }
                  

                  强制转换是unchecked 以防止溢出异常.

                  The casting is unchecked to prevent overflow exceptions.

                  希望这对某人有所帮助.

                  Hope this helps someone.

                  这篇关于如何在实体框架中使用 unsigned int/long 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)

                2. <legend id='xeldH'><style id='xeldH'><dir id='xeldH'><q id='xeldH'></q></dir></style></legend>
                    <bdo id='xeldH'></bdo><ul id='xeldH'></ul>

                        <tbody id='xeldH'></tbody>
                      1. <tfoot id='xeldH'></tfoot>

                            <i id='xeldH'><tr id='xeldH'><dt id='xeldH'><q id='xeldH'><span id='xeldH'><b id='xeldH'><form id='xeldH'><ins id='xeldH'></ins><ul id='xeldH'></ul><sub id='xeldH'></sub></form><legend id='xeldH'></legend><bdo id='xeldH'><pre id='xeldH'><center id='xeldH'></center></pre></bdo></b><th id='xeldH'></th></span></q></dt></tr></i><div id='xeldH'><tfoot id='xeldH'></tfoot><dl id='xeldH'><fieldset id='xeldH'></fieldset></dl></div>

                            <small id='xeldH'></small><noframes id='xeldH'>