对象不生成动态代理

时间:2013-11-07 22:37:28

标签: entity-framework c#-4.0 entity-framework-5

我有一个项目,我的一些对象正在生成DyanmicProxy类,并且正在跟踪它们的更改,而某些对象则没有。

示例:类别安全性 - 这不生成动态代理。 (我有截图,但它会让我发布)

tempObject {Workflow.Data.Access.CategorySecurity} - no Dynamic Proxy appended.

示例2:MenuItem - 这是生成动态代理。

tempObject {System.Data.Entity.DyanmicProxies.MenuItem_782678127361273612738818AEDD878723827}

菜单项 - 有效

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Data.Entity;
using Workflow.Data.Communication;

namespace Workflow.Data.Access
{

[Table("MenuItem", Schema = "wfa")]
public class MenuItem
{
    [Key]
    [ScaffoldColumn(false)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required]
    [DisplayName("Name")]
    [MaxLength(250)]
    public string Name { get; internal set; }

    [Required]
    [DisplayName("Display Name")]
    [MaxLength(250)]
    public string DisplayName { get; set; }

    [Required]
    [DisplayName("Visible")]
    public bool Visible { get; set; }

    [Required]
    [DisplayName("Order")]
    public int Order { get; set; }

    [DisplayName("URL")]
    public string URL { get; set; }

    [DisplayName("Browser Target")]
    public string BrowserTarget { get; set; }

    [DisplayName("Menu")]
    [InverseProperty("MenuItems")]
    [UIHint("WorkflowMenu")]
    public virtual Menu Menu { get; set; }

    [DisplayName("Parent Item")]
    [InverseProperty("ChildMenuItems")]
    public virtual MenuItem ParentMenuItem { get; set; }

    [DisplayName("Menu Items")]
    [InverseProperty("ParentMenuItem")]
    public virtual ICollection<MenuItem> ChildMenuItems { get; internal set; }

    [DisplayName("Responsibilites View Access")]
    [InverseProperty("MenuItems")]
    public virtual ICollection<Responsibility> ViewAccess { get; internal set; }

    [Required]
    [DisplayName("Last Update Date")]
    [ScaffoldColumn(false)]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime LastUpdateDate { get; set; }

    [Required]
    [ScaffoldColumn(false)]
    public int LastUpdateUserID { get; set; }


    [DisplayName("Last Update Date")]
    [ForeignKey("LastUpdateUserID")]
    public virtual WorkflowUser LastUpdateUser { get; set; }

    public MenuItem()
    {
        this.ChildMenuItems = new List<MenuItem>();
        this.ViewAccess = new List<Responsibility>();

    }
}
}

CATEGORY SECUIRTY - 没有工作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Workflow.Data.Template;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace Workflow.Data.Access
{
/// <summary>
/// Defines a Security object specific to a Category
/// </summary>
public class CategorySecurity : Security
{
    [DisplayName("Category")]
    //[InverseProperty("Permissions")]
    [ForeignKey("CategoryID")]
    public virtual Category Category { get; set; }

    public int CategoryID { get; set; }

    public CategorySecurity() : base() { }
}
}

安全 - 父母:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using Workflow.Data.Enum;
using System.Data.Entity;

namespace Workflow.Data.Access
{
/// <summary>
/// Defines a Security in the workflow, which will have a type to define the level of access and a List or Responsibility objects that have that access.
/// </summary>
[Table("Security", Schema = "wfa")]
public class Security
{
    [Key]
    [ScaffoldColumn(false)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required]
    public int TypeID { get; set; }

    [DisplayName("Type")]
    public SecurityType Type { 
        get
        {
            return (SecurityType)TypeID;
        }

        set
        {
            TypeID = (int)value;
        }
    }

    [Required]
    [DisplayName("Responsibilities")]
    [InverseProperty("Security")]
    public virtual ICollection<Responsibility> Responsibilities { get; set; }

    public Security()
    {
        this.Responsibilities = new List<Responsibility>();
    }


}
}

1 个答案:

答案 0 :(得分:0)

所以我的问题围绕着将POCO类创建到DynamicProxy中所需的“规则”。我有几件事是错的;例如,我没有空的get; set;我有几套标记为内部的。

我所做的改变:

  • 将[NotMapped]属性放在非空的gets和sets上。
  • 有保证的:
    • 上课是公开的
    • 所有收藏品均为ICollection
    • 所有获取和设置都是公开的
    • 我也懒得加载所有收藏品,但我认为不应该有所作为

以下是一篇帮助我的好文章:http://blog.oneunicorn.com/2011/12/05/should-you-use-entity-framework-change-tracking-proxies/

干杯!

相关问题