EF 4.1字节[]未从DbContext加载

时间:2011-10-20 04:56:13

标签: .net-4.0 entity-framework-4.1

我有2个实体。

public class Foo
{
   public virtual int FooId {get; set; }
   public virtual IList<Bar> Bars { get; set; }
   public virtual DateTime Date { get; set; }
}

public class Bar
{
   public virtual int BarId { get; set;}
   public virtual byte[] Value { get; set; }
   public virtual DateTime Date { get; set; }
   public virtual IList<Foo> Foos { get; set; }
}

当我通过FooId从数据库加载Foo时,它完全是水合的,当我导航到Bar时,它具有来自数据库的正确BarId和Date,但Value总是一个字节[0]。

为什么呢?

数据库是varbinary(300)列。

如果我在Management Studio中select * from Bar显示

,则数据库中的值
BarId      Value            Date
 1      0x20CF30467ABD   10/19/2011

思想?

我的映射:

public class FooConfiguration : EntityTypeConfiguration<Foo>
{
    public FooConfiguration()
    {
       ToTable("Foo");

       HasKey(m => m.FooId);
       Property(m => m.Date);

        HasMany(m => m.Bars)
                .WithMany(l => l.Foos)
                .Map(m =>
                         {
                             m.ToTable("FooBars");
                             m.MapLeftKey("FooId");
                             m.MapRightKey("BarId");
                         });
    }
}
public class BarConfiguration : EntityTypeConfiguration<Bar>
{
    public BarConfiguration()
    {
       ToTable("Bar");

       HasKey(m => m.BarId);
       Property(m => m.Value);
       Property(m => m.Date);

        HasMany(m => m.Foos)
                .WithMany(l => l.Bars)
                .Map(m =>
                         {
                             m.ToTable("FooBars");
                             m.MapLeftKey("BarId");
                             m.MapRightKey("FooId");
                         });
    }
}

1 个答案:

答案 0 :(得分:1)

我重构了一下你的代码,但我看不出你的问题。

public class Foo
{
    public Foo()
    {
        Bars = new List<Bar>();
    }

    #region Public Properties

    public virtual IList<Bar> Bars { get; set; }

    public virtual DateTime Date { get; set; }

    public virtual int FooId { get; set; }

    #endregion
}

public class Bar
{
    #region Public Properties

    public virtual int BarId { get; set; }

    public virtual DateTime Date { get; set; }

    public virtual IList<Foo> Foos { get; set; }

    public virtual byte[] Value { get; set; }

    #endregion
}

public class FooConfiguration : EntityTypeConfiguration<Foo>
{
    public FooConfiguration()
    {
        HasKey(m => m.FooId);

        HasMany(m => m.Bars)
                .WithMany(l => l.Foos)
                .Map(m =>
                    { 
                        m.ToTable("FooBars");
                        m.MapLeftKey(f => f.FooId, "FooId");
                        m.MapRightKey(b => b.BarId, "BarId");
                    });
    }
}
public class BarConfiguration : EntityTypeConfiguration<Bar>
{
    public BarConfiguration()
    {
        HasKey(m => m.BarId);
    }
}

当我这样做时,我从数据库中获取byte []

using(var context = new FooBarContext())
        {
            var foo = new Foo();
            foo.Date = DateTime.Now;

            var bar = new Bar();
            bar.Date = DateTime.Now;
            bar.Value = UTF8Encoding.UTF8.GetBytes("string");

            foo.Bars.Add(bar);

            context.Foos.Add(foo);

            context.SaveChanges();
        }

        using(var context = new FooBarContext())
        {
            var foos = context.Foos.Where(f => f.FooId == 1).ToList();
        }

确保使用最新的EF。