如何在实体框架数据上下文中的属性名称中使用sql reserved关键字?

kmp*_*kmp 9 .net c# poco code-first entity-framework-4.3

以下代码抛出异常" EntitySqlException:'Group'是保留关键字,除非被转义,否则不能用作别名.在第1行第11列附近 ".

我的问题首先是为什么我在数据上下文中选择的集合的名称和seeminlgy生成的sql查询之间有任何关系?

其次,有什么我可以做的,除了在我的上下文中重命名属性,解决它(我知道名字是愚蠢的,有理由我不能改变名称,就像我想的那样,我赢了'进入这里)?

我可以用modelBuilder做些什么吗?

public class GroupEntity
{
    public int GroupEntityId { get; set; }
    public string Name { get; set; }
}

public class MyContext : DbContext
{
    public MyContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
        Group = Set<GroupEntity>();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<GroupEntity>().ToTable("GroupEntities");
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<GroupEntity> Group { get; private set; }
}

//...
using (var ctx = new MyContext("valid connection string"))
{
    var e = ctx.Group.Count(a => a.GroupEntityId % 2 == 0);
    // Exception thrown here
    Console.WriteLine(e);
}
Run Code Online (Sandbox Code Playgroud)

Art*_*ers 1

此问题已在 EF 4.3.1 和 EF 5.0 beta1 中修复。请使用NuGet将EntityFramework包更新到最新版本。例如,在包管理器控制台中,运行:

Update-Package EntityFramework
Run Code Online (Sandbox Code Playgroud)