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

时间:2011-03-01 19:25:33

标签: wcf entity-framework datacontractserializer

我有一个Restaurant对象,其中包含MenuMenu包含MenuItems

通过实体框架代码首先,我创建了一个数据库并在其中存储了一个Restaurant,其中只有一个MenuItem。我检查了数据库,MenuItem就在那里。但是,当我检索Restaurant对象时,它没有被加载。

我尝试为[OnSerializing]对象实现Menu属性(因为这发生在WCF应用程序中),以便“强制”加载MenuItems,但是没有任何影响。我也看到人们推荐[IncludeAttribute],但该属性存在于两个程序集中,据我所知,我的机器上都没有这个属性。

我试图打开Entity框架的日志记录/跟踪,但到目前为止还没有成功。

                                                                        

无论如何,这是我如何定义我的数据对象:

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

2 个答案:

答案 0 :(得分:1)

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

this.Configuration.ProxyCreationEnabled = false;

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

答案 1 :(得分:0)

如果您首先使用代码进行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; }
}