EF 4.1 byte [] не загружается из DbContext - PullRequest
1 голос
/ 20 октября 2011

У меня есть 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; }
}

Когда я загружаю Foo из базы данных с помощью FooId, он полностью гидратируется, когда я перехожу к Bar, он имеет правильный BarId и Date из базы данных, но значение всегда является байтом [0].

Почему?

База данных - это столбец varbinary (300).

Значение в базе данных, если я сделаю select * from Bar в Management Studio, показывает

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 Ответ

1 голос
/ 22 октября 2011

Я немного реорганизовал ваш код, но не вижу вашей проблемы.

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

Когда я делаю это, я получаю байт [] из базы данных

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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...