A β€œgroup” is a reserved keyword and cannot be used as an alias unless it is escaped

I am using Entity Framework 4.1 with repository template (database already exists). My problem is the existence of the GROUP table (which is reserved). This is a production database that I cannot change.

So, using all of these methods above, I get the following error:

A β€œgroup” is a reserved keyword and cannot be used as an alias unless it is escaped.

Can I tell the Entity Framework to use the following as the table name: [GROUP]

EDIT A class with a db context is as follows (removed)

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

+6
source share
2 answers

Well, that seems very strange, but pay attention to the property name above: the name is Group and it should read groups! It is for this reason that I get this error. The corrected code is as follows:

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

Now works like a charm!

+1
source

Try using a different name for your class and ask it to use the Group table in your database as follows:

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

Or with attributes directly in the entity class:

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

Source: https://habr.com/ru/post/898096/


All Articles