简体   繁体   中英

Navigation Properties are not working properly

I'm facing query a weird issue using the latest version of Entity Framework, regarding navigation properties.

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:

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:

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.

Looking at this, reveals the following error:

"The CreatedBy field is required."

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.

I'm using ASP.NET identity Framework, thus using an IdentityDbContext in case that matters.

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:

 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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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