EF5 Fluent API 字节数组到长 [英] EF5 Fluent API byte array to long

查看:24
本文介绍了EF5 Fluent API 字节数组到长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 EF5 Fluent API 是否有人知道是否可以在数据库中有一个二进制列但在 C# 中有一个很长的列?当我们在实体中放入 long 时,我们总是在运行时遇到 EF 错误(无法执行映射).如果我们放一个 byte[] 那么一切正常(db 中的二进制通常意味着 .NET 代码中的 byte[] 类型).我们无法更改数据库列类型,因此这不是解决方案.

Using EF5 Fluent API does anyone know if it's possible to have a binary column in the database but a long in the C#? When we put a long in the entity we always end up with EF errors at runtime (unable to perform the mapping). If we put a byte[] then everything works (binary in db usually means byte[] type in .NET code). We can't change database column type so it's not a solution.

这是我们最终要做的:

from l in LeadDataRepository.GetAll()
select new { // we need an anonymous type here we use Linq to Entities
    FirstName = l.FirstName,
    LastName = l.LastName,
    CompanyName = l.CompanyName,
    CityId = l.CityId,
    DbID = l.DbId
 }).ToList() // once listed we use Linq to objects
.Select(l => new LeadListingViewModel() { // our real class
    FirstName = l.FirstName,
    LastName = l.LastName,
    CompanyName = l.CompanyName,
    CityId = l.CityId.ToLong(), // here we use our extension method on byte[] which converts to long
    DbID = l.DbId.ToLong()
})

如果我们能够在实体中指定 CityId 是 long(而不是 byte[])并且 DbId 也是如此,那么我们就不必执行所有这些多余的代码.因此这是不可能的,EF 在运行时抱怨(因为 db 列类型是二进制的).但是 SQL Server 处理从二进制到 bigint 的隐式转换...

If we were able to specify in the entity that CityId is a long (and not a byte[]) and the same for DbId then we wouldn't have to do all this redondant code. Therefore this is not possible, EF complains at runtime (because the db column type is binary). But SQL Server handles implicit conversions from binary to bigint...

推荐答案

你可以使用 BitConverter

static void Main(string[] args)
{
    long number = 1234167237613;

    //convert to byte array
    byte[] bytes = BitConverter.GetBytes(number);

    //vice-versa
    long number2 = BitConverter.ToInt64(bytes, 0);

    Console.WriteLine(number == number2);
}

好的,我明白你的问题了.您需要的是一个 Fluent API,它可以自动将 byte[] 转换为 long,反之亦然.很遗憾,Fluent API 无法为您进行转换.

Okay, I've understand your problem. What you need is a Fluent API that does automatic conversion of byte[] to long and vice-versa for you. Unfortunately, Fluent API cannot do conversions for you.

但幸运的是,您可以拥有一个表示 byte[](二进制)属性的包装器属性,该属性仅供您的 C# 代码使用.您只需将此包装器属性标记为 [NotMapped] 以便它不属于您的数据库架构的一部分.然后,每次需要修改此二进制数据时,您只需使用该包装器属性即可.

But fortunately, you can have a wrapper property representing that byte[] (binary) property which is just for your C# code's use. You just have to mark this wrapper property as [NotMapped] in order for it not be part of your DB schema. Then you just have to use that wrapper property every time you need to modify this binary data.

这是一个例子;

namespace EntityFrameworkByteToLong.Models
{
    public class SomeEntity
    {
        public int Id { get; set; }

        public byte[] SomeBytes { get; set; } //this is the column in your DB that can't be changed

        [NotMapped]
        public long SomeByteWrapper //your wrapper obviously
        {
            get
            {
                return BitConverter.ToInt64(SomeBytes, 0);
            }
            set
            {
                SomeBytes = BitConverter.GetBytes(value);
            }
        }
    }
}

然后您可以通过以下方式使用该包装器:

Then you can use that wrapper by:

        using(var ctx = new UsersContext())
        {
            SomeEntity s = new SomeEntity();
            s.SomeByteWrapper = 123861234123;

            ctx.SomeEntities.Add(s);
            ctx.SaveChanges();
        }

这篇关于EF5 Fluent API 字节数组到长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆