Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 514
Savvas Sopiadis Avatar asked Sep 26 '11 18:09

Savvas Sopiadis


People also ask

Is group a reserved word in SQL?

Because group is very common I hoped that it was possible to use it as other common words (e.g. date), but... it is a reserved word in SQL and it is also an ActiveRecord method (i.e. Model. group(*args) ).

How do you escape a reserved keyword in SQL?

To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').

What are Keywords in MySQL?

Keywords are words that have significance in SQL. Certain keywords, such as SELECT , DELETE , or BIGINT , are reserved and require special treatment for use as identifiers such as table and column names. This may also be true for the names of built-in functions.


1 Answers

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!

like image 119
Savvas Sopiadis Avatar answered Nov 15 '22 05:11

Savvas Sopiadis