How to use unsigned int / long types with Entity Framework?

entity-framework entity-framework-6 mysql

Question

class attributes utilizing thelong When adding a new migration (code-first), data types are correctly mapped, butulong The EF provider for MySQL skips over certain data types. How does one use MySQL's property mapping feature?unsigned bigint ?

1
24
10/18/2014 3:25:18 AM

Accepted Answer

Entity Framework does not appear to supportunsigned data formats. Foruint the value might simply be kept in a signed data type with a wider range (that is, along (How aboutulong columns? I couldn't use the standard fix since there isn't an EF-supported signed data type that can accommodate aulong without being bursting.

After some consideration, I came up with a straightforward answer to this issue: simply keep the information in the acceptedlong cast it to after you typeulong upon access. But wait, ulong's maximum value exceeds long's maximum value, you could be saying. Since both have 8 bytes, you can still store an ulong's bytes in a long and cast them back to an ulong when you need them. This will enable you to use EF to store an ulong variable to a database.

// 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;
        }
    }
}

It is a casting.unchecked to avoid overflow errors.

Hope someone can use this.

42
9/9/2016 8:04:31 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow