使用实体框架代码优先的空集合

发布于 2024-10-19 19:18:56 字数 2762 浏览 5 评论 0原文

我有一个 Restaurant 对象,其中包含一个 MenuMenu 包含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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住你手 2024-10-26 19:18:56

如果您在 WCF 上使用 Entity Framework 4.1,请将其放入 DbContext 构造函数中。

this.Configuration.ProxyCreationEnabled = false;

如果您使用 4.0,请使用 ContextOptions.ProxyCreationEnabled = false

If you are using Entity Framework 4.1 on WCF, put this in your DbContext constructor

this.Configuration.ProxyCreationEnabled = false;

If you are using 4.0, use ContextOptions.ProxyCreationEnabled = false

池木 2024-10-26 19:18:56

如果您首先使用 EF 代码
通常,您会将您的物品收藏标记为虚拟。

例子



public class MenuContext : DbContext
{
    public DbSet Menues{ get; set; }
    public DbSet MenuItems { get; set; }
}
public class Menu
{
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGenerationOption.Identity)]
    public Guid MenuID { get; set; }
    public virtual ICollection MenuItems { get; set; }
}

public class MenuItem
{
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGenerationOption.Identity)]
    public Guid MenuItemID { get; set; }
    public string Name { get; set; }
    public int price { get; set; }
}

If your using the Code first for EF
Typically you will mark your collection of items as virtual.

Example



public class MenuContext : DbContext
{
    public DbSet Menues{ get; set; }
    public DbSet MenuItems { get; set; }
}
public class Menu
{
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGenerationOption.Identity)]
    public Guid MenuID { get; set; }
    public virtual ICollection MenuItems { get; set; }
}

public class MenuItem
{
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGenerationOption.Identity)]
    public Guid MenuItemID { get; set; }
    public string Name { get; set; }
    public int price { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文