Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a query type with the same name already exists

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");
    }
}
like image 792
spottedmahn Avatar asked Jul 25 '19 15:07

spottedmahn


2 Answers

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

public class MyContext : DbContext
{
    //DbSet not DbQuery
    public DbSet<MyType> MyTypes { get; set; }
}
like image 182
spottedmahn Avatar answered Oct 13 '22 14:10

spottedmahn


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

like image 27
Eric Avatar answered Oct 13 '22 13:10

Eric