.net 4.0 EF 4.1字节[]未从DbContext加载
我有两个实体.net 4.0 EF 4.1字节[]未从DbContext加载,.net-4.0,entity-framework-4.1,.net 4.0,Entity Framework 4.1,我有两个实体 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 by
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; }
}
想法
我的映射:
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");
});
}
}
公共类FooConfiguration:EntityTypeConfiguration
{
公共配置()
{
ToTable(“Foo”);
HasKey(m=>m.FooId);
财产(m=>m.Date);
有许多(m=>m.bar)
.WithMany(l=>l.Foos)
.Map(m=>
{
m、 ToTable(“FooBars”);
m、 MapLeftKey(“FooId”);
m、 MapRightKey(“BarId”);
});
}
}
公共类配置:EntityTypeConfiguration
{
公共配置()
{
可折叠(“条形”);
HasKey(m=>m.BarId);
属性(m=>m.Value);
财产(m=>m.Date);
HasMany(m=>m.Foos)
.有许多(l=>l.bar)
.Map(m=>
{
m、 ToTable(“FooBars”);
m、 MapLeftKey(“BarId”);
m、 MapRightKey(“FooId”);
});
}
}
我对您的代码进行了一些重构,但看不出您的问题
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。我对您的代码进行了一些重构,但我看不出您的问题
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。我注意到您的配置只映射Key属性,而不映射任何其他属性。我假设这是因为默认情况下EF会尝试将属性名映射到列名,对吗?此外,您的关联仅在Foo配置中设置,而不是在条形图中设置。我将尝试删除我额外的属性映射,看看这是否有什么不同。没错。如果属性与数据库中的名称相同,则不需要映射。如果您在一个实体上定义关联,那么就不需要在另一个实体上定义关联。最后,我们开始尝试一下。您是对的,即使没有您对模型配置所做的重构,它也可以工作。我想我需要进一步研究我的实际映射,看看那里发生了什么。再次感谢。我刚刚将我的项目升级到EF 4.2,没有更改任何代码,它成功了。想知道EF 4.1是否有什么原因导致了它。谢谢我注意到您的配置只映射密钥属性,而不映射任何其他属性。我假设这是因为默认情况下EF会尝试将属性名映射到列名,对吗?此外,您的关联仅在Foo配置中设置,而不是在条形图中设置。我将尝试删除我额外的属性映射,看看这是否有什么不同。没错。如果属性与数据库中的名称相同,则不需要映射。如果您在一个实体上定义关联,那么就不需要在另一个实体上定义关联。最后,我们开始尝试一下。您是对的,即使没有您对模型配置所做的重构,它也可以工作。我想我需要进一步研究我的实际映射,看看那里发生了什么。再次感谢。我刚刚将我的项目升级到EF 4.2,没有更改任何代码,它成功了。想知道EF 4.1是否有什么原因导致了它。谢谢
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();
}