已存在同名的查询类型

spo*_*ahn 6 entity-framework-core

无法将实体类型“MyType”添加到模型中,因为已存在同名的查询类型。

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

spo*_*ahn 9

更改DbQueryDbSet查询类型用于视图

public class MyContext : DbContext
{
    //DbSet not DbQuery
    public DbSet<MyType> MyTypes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)