简体   繁体   English

导航属性无法正常工作

[英]Navigation Properties are not working properly

I'm facing query a weird issue using the latest version of Entity Framework, regarding navigation properties. 我正在使用有关导航属性的最新版本的Entity Framework查询一个奇怪的问题。

I do have an entity of which I have a few required navigation properties which are marked as virtual. 我确实有一个实体,其中有一些必需的导航属性被标记为虚拟。 See my entities class below: 请参阅下面的我的实体类:

public class Folder : UserReferencedEntityBase<int>
{
    #region Constructors

    public Folder()
    { }

    public Folder(IUnitOfWork unitOfWork)
        : base(unitOfWork)
    {
        ParentFolder = unitOfWork.Context.GetCurrentFolder as Folder;
    }

    #endregion

    #region Properties

    [Required]
    public string Name { get; set; }

    [Required]
    public string Data { get; set; }

    [Column(Order = 998)]
    public Folder ParentFolder { get; set; }

    [Required]
    public bool IsPublished { get; set; }

    #endregion
}

This one is inheriting from UserReferencedEntityBase{T} which looks like: 这是从UserReferencedEntityBase{T}继承的,看起来像:

public class UserReferencedEntityBase<TKey> : EntityBase<TKey>
{
    #region Constructors

    public UserReferencedEntityBase() { } 

    public UserReferencedEntityBase(IUnitOfWork unitOfWork)
    {
        unitOfWork.ThrowIfNull("unitOfWork");

        CreatedBy = unitOfWork.Context.GetCurrentUser;
    }

    #endregion

    #region Properties

    [Required]
    [Column(Order = 996)]
    public virtual IdentityUser CreatedBy { get; set; }

    [Column(Order = 997)]
    public virtual IdentityUser UpdatedBy { get; set; }

    #endregion
}

Now, I do have my MVC website where I'm loading an entity, updating a property and saving it in the database again: 现在,我的确是在MVC网站上,我正在其中加载实体,更新属性并将其再次保存在数据库中:

var model = new FolderManager(UnitOfWork).GetFolder(id);
model.IsPublished = true;
UnitOfWork.Commit();

I use a custom Unit Of Work here, but no rocket sience at all. 我在这里使用自定义的工作单位,但是根本没有火箭的声音。 Everything is happening with the same context, within the same request, no async calls, ... 一切都在相同的上下文中发生,在相同的请求内,没有异步调用,...

When I do execute the code, I receive: 当我执行代码时,我收到:

Validation failed for one or more entities. 一个或多个实体的验证失败。 See 'EntityValidationErrors' property for more details. 有关更多详细信息,请参见'EntityValidationErrors'属性。

Looking at this, reveals the following error: 看看这个,发现以下错误:

"The CreatedBy field is required." “ CreatedBy字段为必填。”

Now, the weird this is, when I'm debugging my code, the 3 lines given above, the created_by property is filled in and the code does execute without any problem. 现在,这很奇怪,当我调试我的代码时,上面给出的3行代码被填充,created_by属性被填充并且代码确实执行没有任何问题。

I'm using ASP.NET identity Framework, thus using an IdentityDbContext in case that matters. 我正在使用ASP.NET身份框架,因此在需要时使用IdentityDbContext。

Anybody has a clue? 有人知道吗?

Kind regards 亲切的问候

UPDATE - Folder Manager 更新-文件夹管理器

The manager is just a wrapper to get my contents out of my unit of work: 经理只是将我的内容从工作单元中取出的包装器:

public Folder GetFolder(int id)
{
    return UnitOfWork.FolderRepository.GetByFilter(x => x.Id == id);
}

The GetByFilter method is constructed like: GetByFilter方法的构造如下:

 public virtual TEntity GetByFilter(Func<TEntity, bool> filter)
 {
     DbSet.ThrowIfNull("DbSet");

     if (OnBeforeEntityGet != null)
     { OnBeforeEntityGet(this, new RepositoryEventArgs(typeof(TEntity))); }

     if (OnEntityGet != null)
     { OnEntityGet(this, new RepositoryEventArgs(typeof(TEntity))); }

     return !Entities.Any() ? null : !Entities.Where(filter).Any() ? null : Entities.First(filter);
 }

Just want to let you know that I've found a solution. 只想让您知道我已经找到了解决方案。 It seems that when you're loading an entity that contains virtual properties but never inspecting them, they stay null and therefore the code is not working. 看起来,当您加载包含虚拟属性的实体但从不检查它们时,它们保持为空,因此代码无法正常工作。

With the debugger is attached, it's working flaslessy after inspecting this element. 附加了调试器后,检查该元素后,它就可以轻松运行了。

Is this normal behaviour? 这是正常行为吗?

Kind regards, 亲切的问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM