0

我有一个项目,其中我的一些对象正在生成 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>();

    }
}
}

类别安全 - 不起作用

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


}
}
4

1 回答 1

0

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

我所做的更改:

  • 将 [NotMapped] 属性放在非空的获取和集合上。
  • 确保:
    • 上课是公开的
    • 所有收藏都是ICollection
    • 所有获取和设置都是公开的
    • 我也懒加载所有集合,但我认为这不会有所作为

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

干杯!

于 2013-11-08T13:42:31.023 回答