EF Unable to determine the relationship represented by navigation property

asp.net-core c# entity-framework-core

Question

When i try to run this code i get an exception from EntityFramework that it is Unable to determine the relationship represented by navigation property 'Workflow1.Step1' of type 'Step1'. I have tried to blindly configure it in OnModelCreating with no luck, any idea how to solve this?

The code works fine if i remove DbQuery but i want it there so i can Query on that data without doing an Include from a workflow, and still be sure that no changes are made directly inside the step.

public class Workflow1
{
        public Guid Id { get; private set; }

        public Guid Step1Id {get; private set;}
        public Step1 Step1 {get; private set;}

        public Guid Step2Id {get; private set;}
        public Step2 Step2 {get; private set;}

        public Guid Step3Id {get; private set;}
        public Step3 Step3 {get; private set;}
}

public class Workflow2
{
        public Guid Step2Id {get; private set;}
        public Step2 Step2 {get; private set;}

        public Guid Step4Id {get; private set;}
        public Step5 Step4 {get; private set;}
}

public class Step1 
{
    public Guid Id { get; private set; }
}

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) :base(options)
    {}

    public DbSet<Workflow1> Workflow1 { get; set; }
    public DbSet<Workflow2> Workflow2 { get; set; }

    public DbQuery<Step1> Step1 { get; set; }
    public DbQuery<Step3> Step2 { get; set; }
    public DbQuery<Step3> Step3 { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}
1
0
12/10/2018 5:28:23 PM

Accepted Answer

The code works fine if i remove DbQuery but i want it there so i can Query on that data without doing an Include from a workflow, and still be sure that no changes are made directly inside the step.

No! You cannot do that because EF Core Query Type documentation clearly says that:

Entities cannot contain navigation properties to query types.

So for mapping you have to make Step1,Step2, Step3 as DbSet<> because Workflow1 is DbSet<> or Entity Type type.

1
12/10/2018 5:38:32 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow