使用实体框架代码优先的空集合
我有一个 Restaurant
对象,其中包含一个 Menu
。 Menu
包含MenuItems
。
通过实体框架 Code First,我创建了一个数据库并在其中存储了一个 Restaurant
,其中有一个 MenuItem
。我检查了数据库,MenuItem
就在那里。但是,当我检索 Restaurant
对象时,它没有被加载。
我尝试为 Menu
对象实现 [OnSerializing]
属性(因为这是在 WCF 应用程序中发生的),以便“强制”MenuItems< /code> 已加载,但这没有任何效果。我还看到有人推荐
[IncludeAttribute]
,但该属性存在于两个程序集中,据我所知,这两个程序集中都不存在于我的计算机上。
我尝试打开实体框架的日志记录/跟踪,但到目前为止没有成功。
无论如何,这是我定义数据对象的方式:
[DataContract]
public class MenuItem
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string Price { get; set; }
}
[CollectionDataContract]
public class ListOfMenuItem : List<MenuItem>
{
}
[DataContract]
public class Menu
{
/// <summary>
/// Alternate constructor, used during serialization operation.
/// </summary>
/// <param name="pContext"></param>
[OnDeserializing]
public void OnDeserializing(StreamingContext pContext)
{
Init();
}
public Menu()
{
Init();
}
private void Init()
{
MenuItems = new ListOfMenuItem();
}
[DataMember]
public int ID { get; set; }
[DataMember]
public ListOfMenuItem MenuItems
{
get;
set;
}
}
[DataContract]
public class Restaurant
{
/// <summary>
/// Alternate constructor, used during serialization operation.
/// </summary>
/// <param name="pContext"></param>
[OnDeserializing]
public void OnDeserializing(StreamingContext pContext)
{
Init();
}
public Restaurant()
{
Init();
}
private void Init()
{
Hours = new HoursOfOperation();
Menu = new Menu();
}
/// <summary>
/// Unique name and identifier for a restaurant.
/// </summary>
[DataMember(IsRequired = true)]
[Key]
public string Name
{
get;
set;
}
/// <summary>
/// What hours is the restaurant open.
/// </summary>
[DataMember]
public HoursOfOperation Hours
{
get;
set;
}
/// <summary>
/// What does the restaurant have to eat and drink.
/// </summary>
[DataMember]
public Menu Menu
{
get;
set;
}
}
数据库上下文定义为:
public class RestaurantDirectory : DbContext
{
public DbSet<Restaurant> Restaurants { get; set; }
}
I have a Restaurant
object, which contains a Menu
. The Menu
contains MenuItems
.
Via the Entity Framework Code First, I created a database and stored a single Restaurant
in it, which has a single MenuItem
. I've checked the database, and the MenuItem
is there. However, it isn't getting loaded when I retrieve the Restaurant
object.
I tried implementing the [OnSerializing]
attribute for the Menu
object (since this is taking place in a WCF application), so as to "force" the MenuItems
to be loaded, but that didn't have any effect. I've also seen folks recommending the [IncludeAttribute]
, but that attribute exists in two assemblies, neither of which is present on my machine as far as I can tell.
I've attempted to turn on logging/tracing for the Entity framework, but so far without success.
Anyway, here is how I've got my data objects defined:
[DataContract]
public class MenuItem
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string Price { get; set; }
}
[CollectionDataContract]
public class ListOfMenuItem : List<MenuItem>
{
}
[DataContract]
public class Menu
{
/// <summary>
/// Alternate constructor, used during serialization operation.
/// </summary>
/// <param name="pContext"></param>
[OnDeserializing]
public void OnDeserializing(StreamingContext pContext)
{
Init();
}
public Menu()
{
Init();
}
private void Init()
{
MenuItems = new ListOfMenuItem();
}
[DataMember]
public int ID { get; set; }
[DataMember]
public ListOfMenuItem MenuItems
{
get;
set;
}
}
[DataContract]
public class Restaurant
{
/// <summary>
/// Alternate constructor, used during serialization operation.
/// </summary>
/// <param name="pContext"></param>
[OnDeserializing]
public void OnDeserializing(StreamingContext pContext)
{
Init();
}
public Restaurant()
{
Init();
}
private void Init()
{
Hours = new HoursOfOperation();
Menu = new Menu();
}
/// <summary>
/// Unique name and identifier for a restaurant.
/// </summary>
[DataMember(IsRequired = true)]
[Key]
public string Name
{
get;
set;
}
/// <summary>
/// What hours is the restaurant open.
/// </summary>
[DataMember]
public HoursOfOperation Hours
{
get;
set;
}
/// <summary>
/// What does the restaurant have to eat and drink.
/// </summary>
[DataMember]
public Menu Menu
{
get;
set;
}
}
And the database context is defined as:
public class RestaurantDirectory : DbContext
{
public DbSet<Restaurant> Restaurants { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 WCF 上使用 Entity Framework 4.1,请将其放入 DbContext 构造函数中。
如果您使用 4.0,请使用 ContextOptions.ProxyCreationEnabled = false
If you are using Entity Framework 4.1 on WCF, put this in your DbContext constructor
If you are using 4.0, use
ContextOptions.ProxyCreationEnabled = false
如果您首先使用 EF 代码
通常,您会将您的物品收藏标记为虚拟。
例子
If your using the Code first for EF
Typically you will mark your collection of items as virtual.
Example