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");
}
}
Solution 1:
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; }
}
Solution 2:
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