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");
}
}
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; }
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With