Empty collection using Entity Framework First code

I have an object Restaurantthat contains Menu. Menucontains MenuItems.

Through the Code Entity Framework First, I created a database and saved one in it Restaurant, which has one MenuItem. I checked the database, and there is MenuItem. However, when loading an object, Restaurantit does not load.

I tried to implement an attribute [OnSerializing]for an object Menu(since this happens in a WCF application) in order to "force" load MenuItems, but it had no effect. I also saw people recommend it [IncludeAttribute], but this attribute exists in two assemblies, none of which are present on my machine, as far as I can tell.

I tried to enable logging / tracing for the Entity framework, but so far without success.

                                                 

Anyway, here is how I defined the data objects:

[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; }
}
+3
source share
2 answers

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

this.Configuration.ProxyCreationEnabled = false;

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

+1
source

If you first use the code for EF As a rule, you will mark the collection of objects 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
source

Source: https://habr.com/ru/post/1795578/


All Articles