• <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 类型?

        时间:2023-06-03

          • <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 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以用纯 mysql 解决这个问题吗?(加入一列中的 ';' 分隔值) 下一篇:MySQL UPDATE 和 SELECT 一次完成

                  相关文章

                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>

                  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'>