开发者

Empty Collection using Entity Framework Code First

开发者 https://www.devze.com 2023-02-14 08:50 出处:网络
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 MenuI

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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消