代码之家  ›  专栏  ›  技术社区  ›  gsharp

EF Core 3.1至5更新->IEntityType.GetTableName

  •  2
  • gsharp  · 技术社区  · 4 年前

    // Model
    builder.Entity<MyEntity>().HasNoKey().ToView("MyView", "dbo");
    
    // Then I have this Function
    public static GetTableName<T>(this MyDbContext db)
    {
          var t = db.Model.FindEntityType(typeof(T));
          return t.GetTableName()
    }
    

    t.GetTableName() 退货 null 因为应该使用的方法是 t.GetViewName()

    所以我把密码改成

    return t.GetTableName() ?? t.GetViewName()

    不过,我认为它“丑陋”,我宁愿检查是否 t 是视图还是表,然后调用右边的 t.Get[View/Tabe]Name t型

    有什么想法和方法来找出它是一个视图还是一个表?

    0 回复  |  直到 4 年前
        1
  •  3
  •   AndreasHassing    4 年前

    偶数实体框架核心 source is using the GetViewName() method to figure out if an object is a view .

    考虑到这一点,我认为您的解决方案是简洁的,您可以创建自己的扩展方法,完全按照您现有的方法来隐藏“丑陋”:

    public static string GetTableOrViewName(this IEntityType entityType) =>
        entityType.GetTableName() ?? entityType.GetViewName();