6

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

2
  • Who is giving you this error? Is it in Visual Studio or its your database? Can you post the code of your class that has the problem? Sep 26, 2011 at 18:26
  • @AlexandreJobin: i'm getting the error in VS. See also the EDIT. Sep 26, 2011 at 18:54

2 Answers 2

1

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!

4
  • Just found the same and the result seems very weird to me. Even using context.Set<Group>().ToList() works, but context.Group.ToList() doesn't. Strange...
    – Slauma
    Sep 26, 2011 at 19:07
  • So what does this mean ... i'm confused! Can't we choose whatever name we would like for the DBSet?? Sep 26, 2011 at 19:29
  • 1
    So it looks like. I've just tested to give DbSet<Group> names like Select, Join and Where. They all throw the same exception. EF seems to check against a list of SQL keywords. The entity name Group itself is not the problem.
    – Slauma
    Sep 26, 2011 at 19:42
  • You 're right! Having a look at the mapping instruction tells us that the entity name can be ... whatever! BUT the DBSet cannot. Well, learned something new today!! Thank very much! Sep 26, 2011 at 19:47
0

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; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.