2011-10-20 19 views
1

私は2つのエンティティを持っています。EF 4.1バイト[]がDbContextからロードされていません

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をロードすると、それは私がバーに移動すると、それはデータベースから正しいBarIdと日付を持っていますが、値は常にバイトで、完全に水和[0]。

なぜですか?

データベースはvarbinary(300)列です。

データベース内の値Iは、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

私はあなたのコードを少しリファクタリングが、私はあなたの問題を見ることができません。私はこれを行うと

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

は、私はあなたがEFの最新の使用していることを確認し、バックデータベース

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

からバイト[]を取得します。

+0

私の設定では、Keyプロパティのみがマップされており、他のキーマップはマップされていないことがわかりました。これは、デフォルトでEFがプロパティ名を列名にマップしようとしているからだと仮定していますか?また、関連付けは、バーではなくFoo設定でのみ設定されます。私は、特別なプロパティのマッピングを特別に削除し、その違いがあるかどうかを調べるつもりです。 – Adam

+1

そうです。プロパティがdbと同じ名前を持つ場合、マッピングの必要はありません。 1つのエンティティで関連付けを定義する場合は、別のエンティティで関連付けを定義する必要はありません。 –

+0

最後にこれを試してみました。あなたが正しいです、それはあなたがモデル構成に行ったリファクタリングなしでさえ、動作します。私は私の実際のマッピングをもう少し調べ、そこで何が起こっているのかを調べる必要があると思います。再度、感謝します。 – Adam

関連する問題