// 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()
t.GetTableName()
null
t.GetViewName()
所以我把密码改成
return t.GetTableName() ?? t.GetViewName()
不过,我认为它“丑陋”,我宁愿检查是否 t 是视图还是表,然后调用右边的 t.Get[View/Tabe]Name t型
t
t.Get[View/Tabe]Name
t型
有什么想法和方法来找出它是一个视图还是一个表?
偶数实体框架核心 source is using the GetViewName() method to figure out if an object is a view .
GetViewName()
考虑到这一点,我认为您的解决方案是简洁的,您可以创建自己的扩展方法,完全按照您现有的方法来隐藏“丑陋”:
public static string GetTableOrViewName(this IEntityType entityType) => entityType.GetTableName() ?? entityType.GetViewName();