Base solution for your next web application
Open Closed

Getting error when using .GetAllIncluding #4144


User avatar
0
bertusvanzyl created

I attached a project demonstrating the problem. (I left out the Web project, it is too big to upload here)

I am getting the exception I have added below when I call this code on a repository:

var result = this.Repository
                    .GetAllIncluding(x => x.CreatedFrom)
                    .ToList();

If I don't add the Including, it works, but then the CreatedFrom property is not populated. The problem also goes away if I remote the IMustHaveTenant interfaces from the models.

The repository is created like so:

public MyAppService(IRepository<Policy, long> p)
        {
            this.Repository = p;
        }

Context:

public virtual IDbSet<Policy> Policies { get; set; }
        public virtual IDbSet<ClonedPolicyDefinition> ClonedPolicyDefinitions { get; set; }

Models:

public class ClonedPolicyDefinition : Entity<long>, IMustHaveTenant
    {
        public int TenantId { get; set; }
    }

    public class Policy : Entity<long>, IMustHaveTenant
    {
        public int TenantId { get; set; }

        public ClonedPolicyDefinition CreatedFrom { get; set; }
    }

Exception message:

FK Constriant not found for association 'TestProblem1.EntityFramework.Policy_CreatedFrom' - must directly specify foreign keys on model to be able to apply this filter

Stack trace:

at EntityFramework.DynamicFilters.DynamicFilterQueryVisitorCSpace.Visit(DbPropertyExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbPropertyExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at System.Data.Entity.Core.Common.CommandTrees.DefaultExpressionVisitor.VisitExpression(DbExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DefaultExpressionVisitor.VisitList[TElement](IList`1 list, Func`2 map)
   at System.Data.Entity.Core.Common.CommandTrees.DefaultExpressionVisitor.VisitExpressionList(IList`1 list)
   at System.Data.Entity.Core.Common.CommandTrees.DefaultExpressionVisitor.Visit(DbNewInstanceExpression expression)
   at EntityFramework.DynamicFilters.DynamicFilterQueryVisitorCSpace.Visit(DbNewInstanceExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbNewInstanceExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at System.Data.Entity.Core.Common.CommandTrees.DefaultExpressionVisitor.VisitExpression(DbExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DefaultExpressionVisitor.Visit(DbProjectExpression expression)
   at EntityFramework.DynamicFilters.DynamicFilterQueryVisitorCSpace.Visit(DbProjectExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at EntityFramework.DynamicFilters.DynamicFilterInterceptor.TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
   at System.Data.Entity.Infrastructure.Interception.DbCommandTreeDispatcher.<Created>b__0(IDbCommandTreeInterceptor i, DbCommandTreeInterceptionContext c)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](TResult result, TInterceptionContext interceptionContext, Action`2 intercept)
   at System.Data.Entity.Infrastructure.Interception.DbCommandTreeDispatcher.Created(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree)

src.7z


3 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    You need to define foreign key, by convention:

    public class Policy : Entity<long>, IMustHaveTenant
    {
        public int TenantId { get; set; }
    
        public ClonedPolicyDefinition CreatedFrom { get; set; }
    
        public long CreatedFromId { get; set; }
    }
    
  • User Avatar
    0
    bertusvanzyl created

    Is this asp.net boilerplate specific, or an EF6 convention?

  • User Avatar
    0
    bertusvanzyl created

    Nevermind, found it!

    <a class="postlink" href="http://www.entityframeworktutorial.net/code-first/code-first-conventions.aspx">http://www.entityframeworktutorial.net/ ... tions.aspx</a>

    Thank you for your time and effort.