0xb*_*00d 8 entity-framework code-first entity-framework-4 entity-framework-4.1
可能吗?当我指定TableAttribute时,我知道我可以获得名称,但是当我让框架管理名称时,它应该是可能的.
提前致谢.
小智 11
我最终得到了这个:
public static class DbContextExt
{
public static string GetTableName<T>(this DbContext context) where T : class
{
var type = typeof(T);
var entityName = (context as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext.CreateObjectSet<T>().EntitySet.Name;
var tableAttribute = type.GetCustomAttributes(false).OfType<System.ComponentModel.DataAnnotations.Schema.TableAttribute>().FirstOrDefault();
return tableAttribute == null ? entityName : tableAttribute.Name;
}
}
Run Code Online (Sandbox Code Playgroud)
这是两个答案的混合:DBset tabel name.
如果您不使用Fluent api 来定义表名称,则该名称将从上下文中的属性TableAttribute名称推断出来。DbSet在这种情况下,唯一可以修改名称的是默认使用的复数约定。
所以如果你有:
public class Context : DbContext
{
public DbSet<User> Users { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
该表应命名为Users.
| 归档时间: |
|
| 查看次数: |
3420 次 |
| 最近记录: |