开发者

'Group' is a reserved keyword and cannot be used as an alias, unless it is escaped

开发者 https://www.devze.com 2023-04-08 18:12 出处:网络
I\'m using Entity Framework 4.1 with repository pattern (Database is already existing). My problem is the existence of a table called GROUP (which is reserved). This is a production database which i c

I'm using Entity Framework 4.1 with repository pattern (Database is already existing). My problem is the existence of a table called GROUP (which is reserved). This is a production database which i cannot change.

So, using all this techniques above i'm getting the following error:

开发者_运维问答

'Group' is a reserved keyword and cannot be used as an alias, unless it is escaped.

Is it possible to tell Entity Framework to use the following as the table name: [GROUP]

EDIT The class with the db context looks like the following (stripped down)

 public class AMTDatabase : DbContext
    {

      private IDbSet<GROUP> _Groups;
      public IDbSet<GROUP> Group
      {
        get { return _Groups ?? (_Groups = DbSet<GROUP>()); }
      }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {        
      base.OnModelCreating(modelBuilder);
      modelBuilder.Entity<GROUP>().ToTable("GROUP");      
    }
    //etc
    }

Thanks in advance


Well it seems very weird, but notice the property name above: the name is Group and it should read Groups! This is the reason i'm getting this error. The corrected code is the following:

private IDbSet<GROUP> _Groups;
        public IDbSet<GROUP> Groups
        {
            get { return _Groups ?? (_Groups = DbSet<GROUP>()); }
        }

Works like a charm now!


Try to use another naming for you class and tell him to use the Group table in your database like so:

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

Or with the attributes directly on your entity class:

[Table("Group")]
public class MyGroup
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("GroupId")]
    public int GroupId { get; set; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消