2017-03-27 161 views
1

我首先在代码中使用实体框架。我的关系属性继续打破。实体框架导航属性null

我有对象Element

public class Element : IElement 
{ 
    // ... some event handlers (removed) 

    [Key] 
    public Guid ID { get; set; } = Guid.NewGuid(); 

    public string Name { get; set; } 

    // navigation properties 
    public virtual ElementType ElementType { get; private set; } 

    public virtual NotifiableCollection<Property> Properties { get; private set; } = new NotifiableCollection<Property>(); 

    // Parameterless constructor for serialization 
    private Element() { } 

    public Element(ElementType elementType) : base() 
    { 
     // loop through and create Properties for each Property Type 
     ElementType = elementType; 
     if (ElementType?.PropertyTypes != null) 
     { 
      ElementType.PropertyTypes.ToList().ForEach((property) => 
      { 
       Properties.Add(new Property(property)); 
      }); 
     } 
    } 
} 

而且ElementType

public class ElementType : IElementType 
{ 
    // ... some event handlers (removed) 

    [Key] 
    public Guid ID { get; set; } = Guid.NewGuid(); 

    public string Name { get; set; } 

    // navigation properties 
    public virtual NotifiableCollection<PropertyType> PropertyTypes { get; set; } = new NotifiableCollection<PropertyType>(); 

    public virtual NotifiableCollection<Element> Elements { get; set; } = new NotifiableCollection<Element>(); 

    public ElementType() 
    { 
     // ensure our Element's get updates 
     PropertyTypes.CollectionChanged += (e, a) => 
     { 
      //update the database to send out renewal to interested entities 
      if (a.ChangeType == ChangeType.Added) 
      { 
       foreach (Element element in Elements) 
       { 
        element.Properties.Add(new Property(a.Item)); 
       } 
      } 
     }; 
    } 
} 

当我创建这些对象的第一次(因为我已经明确设置导航属性,然后保存它工作正常):

enter image description here

然而,当我再关闭一切,并从数据库中获取这些:

enter image description here

的导航属性都没有解决。该表定义建立foregn键关系罚款:

CREATE TABLE [dbo].[Elements] (
    [ID]    UNIQUEIDENTIFIER NOT NULL, 
    [Name]   NVARCHAR (MAX) NULL, 
    [ElementType_ID] UNIQUEIDENTIFIER NULL, 
    CONSTRAINT [PK_dbo.Elements] PRIMARY KEY CLUSTERED ([ID] ASC), 
    CONSTRAINT [FK_dbo.Elements_dbo.ElementTypes_ElementType_ID] FOREIGN KEY ([ElementType_ID]) REFERENCES [dbo].[ElementTypes] ([ID]) 
); 


GO 
CREATE NONCLUSTERED INDEX [IX_ElementType_ID] 
    ON [dbo].[Elements]([ElementType_ID] ASC); 

,我可以看到的数据是正确的:

ID         Name        ElementType_ID 
ff186746-62cb-4246-9c64-f2d007b23ac0 Aircon Test 27/03/2017 12:54:03 57d93ac1-ad3b-4718-a593-80639cc24907 
其匹配的ElementType表中的ID

我有这一套在我的仓库:

context.Configuration.ProxyCreationEnabled = true; 
context.Configuration.LazyLoadingEnabled = true; 

和上下文仍然活跃在那里我试图解决这个属性的时间。

一切正常,但我已经与EF多次这个问题,我的导航属性只是随机突破。我不记得触及任何与此元素相关的代码,只是运行它,现在它不起作用。谁能帮忙?

编辑:这是库代码:

public sealed class Repository : IRepository 
{ 
    public event ObjectMaterializedEventHandler ObjectMaterialized; 

    public Repository() { 
     (context as IObjectContextAdapter).ObjectContext.ObjectMaterialized += ObjectContext_ObjectMaterialized; ; 
     context.Configuration.ProxyCreationEnabled = true; 
     context.Configuration.LazyLoadingEnabled = true; 
    } 

    // I do this to wire in some events later 
    private void ObjectContext_ObjectMaterialized(object sender, ObjectMaterializedEventArgs e) 
    { 
     ObjectMaterialized?.Invoke(this, e); 
    } 

    private DataContext context = new DataContext(false); 

    public IEnumerable<T> GetAll<T>() where T : class 
    { 
     return context.Set<T>().ToList() as IEnumerable<T>; 
    } 

    public T GetItem<T>(Guid id) where T : class 
    { 
     return context.Set<T>().Find(id) as T; 
    } 
    ... 
} 

的情况下将它们存储这样的:

public class DataContext : DbContext 
{ 
    ... 
    public DbSet<Element> Elements { get; set; } 
    public DbSet<ElementType> ElementTypes { get; set; } 
} 

我认为这是值得做的访问。我正在使用context.Set()来访问元素,发现(id)为T,并且失败。但是,如果我通过ElementTypes进行navagate,找到它的实体列表,那么它工作正常。

+0

您是否重复使用您的EF上下文进行多项操作(即:是否将它保存在某个静态\实例变量中)? – Evk

+0

你能展示如何从数据库获取实体吗?你是否在“使用”块中封装了上下文/存储库? – DavidG

+0

是的,上下文保留在存储库中。我将添加一些代码来显示检索。 – Joe

回答

3

在Ivan的帮助下在评论中找到了答案。

的问题是与具有私有构造函数:

// Parameterless constructor for serialization 
private Element() { } 

一个为proxies的要求是一个公共或受保护的构造:

对于这两种代理要创建:自定义数据类别必须为 ,并声明为公共访问权限。

  • 自定义数据类必须不(在Visual 基本NotInheritable)被密封

  • 自定义数据类不能是抽象的(为MustInherit在Visual 基本)。

  • 自定义数据类必须有一个公共或受保护的构造是 没有参数。如果您希望使用CreateObject方法为POCO实体创建 代理,请使用不带 参数的受保护构造函数。调用CreateObject方法不保证创建代理服务器:POCO类必须遵循本主题中描述的其他要求 。

  • 该类无法实现IEntityWithChangeTracker或 IEntityWithRelationships接口,因为代理类 实现了这些接口。

  • ProxyCreationEnabled选项必须设置为true。

对于延迟加载代理:每个导航属性必须声明为 公开,虚拟(可重写在Visual Basic),以及(在Visual Basic NotOverridable)不密封 get访问。