EF5流利的API字节数组长 [英] EF5 Fluent API byte array to long

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

问题描述

使用EF5流利的API没有人知道是否有可能在数据库中的二进制列,但在C#长?当我们把一个长实体,我们总是在运行时错误EF结束(无法执行映射)。如果我们把一个byte [],然后一切正常(二进制在分贝通常是指在.NET code的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是一个长期的(而不是一个byte [])和相同的DBID那么我们就不必做这一切redondant code中的实体来指定。因此,这是不可能的,EF抱怨在运行时(因为该数据库列类型是二进制)。但是,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

You can just use 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);
}

编辑:

好吧,我明白你的问题。你需要的是一个良好的API,确实字节[] 自动转换为,反之亦然你。不幸的是,流利的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.

但幸运的是,你可以有一个包装产权重组presenting的字节[] (二)财产仅仅是你的C#code的使用。你只需要以这种包装属性作为 [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流利的API字节数组长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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