【问题标题】:a query type with the same name already exists已存在同名查询类型
【发布时间】:2019-07-25 15:02:18
【问题描述】:

无法将实体类型“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");
    }
}

【问题讨论】:

    标签: entity-framework-core


    【解决方案1】:

    DbQuery 更改为DbSetKeyless Entity Types 用于视图,among other things

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

    【讨论】:

      【解决方案2】:

      显然你和我在同一天遇到了同样的问题:)

      我的问题是我的视图设置为 DBset:

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

      但我的映射设置如下:

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

      我的解决方法是做与你所做的相反的事情。我不得不将 DbSet 更改为 DbQuery。

      你的回答帮助我得到了我的:D

      【讨论】:

      • 太棒了!这就是为什么我尝试通过调试和结果与社区分享! ?
      猜你喜欢
      • 1970-01-01
      • 2018-07-12
      • 2011-05-27
      • 2012-10-02
      • 2023-01-23
      • 1970-01-01
      • 2018-09-07
      • 2018-10-09
      • 1970-01-01
      相关资源
      最近更新 更多