a query type with the same name already exists

entity-framework-core

Question

The entity type 'MyType' cannot be added to the model because a query type with the same name already exists.

public class MyContext : DbContext
{
    public DbQuery<MyType> MyTypes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //Exception is thrown here

        //needed b/c table is not named MyTypes
        modelBuilder.Entity<MyType>()
            .ToTable("MyType");
    }
}
1
4
7/26/2019 2:19:55 AM

Accepted Answer

Change DbQuery to DbSet. Query Types are used for Views, among other things.

public class MyContext : DbContext
{
    //DbSet not DbQuery
    public DbSet<MyType> MyTypes { get; set; }
}
6
7/26/2019 2:21:33 AM

Popular Answer

Apparently you and I had the same problem on the same day :)

My issue was that I had my view set up as DBset:

public virtual DbSet<VwVendors> VwVendors{ get; set; }

But my mapping was set up as follows:

modelBuilder.Query<VwVendors>()
    .ToView("vw_Vendors");

My fix was to do the opposite of what you did. I had to change DbSet to DbQuery.

Your answer helped me get mine :D



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