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
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) ).
To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With