开发者

Unicode table name problem with DbContext in Entity Framework 4.1

I thought I'd try the new DbContext way of doing things in EF 4.1, but ran into a problem.

I generate a new entity data model from database (SQL Server 2008) and then I use the DbContext generator.

When I try to fetch the table called Länder i get an exception.

var db = new MyEntities();  // instantiat开发者_运维百科e DbContext subclass
var countries = db.Länder.ToList();  // boom

EntitySqlException: The simple identifier 'Länder' must contain basic Latin characters only. To use UNICODE characters, use an escaped identifier.

If if try other tables with latin names - all is fine. And I don't have this problem when using ObjectContext.

How am I supposed to escape the table name?


We (the EF team) investigated and found that we were not properly escaping (i.e. adding square brackets) for the names of the entity sets when we bootstrap the DbSets.

Thanks a lot for reporting this!

We are currently testing a fix that should be included in a future release. In the meanwhile, you should be able to workaround this by specifying the entity set name explicitly such that it doesn’t contain any characters that would need escaping.

When using Database First or Model First this can be done by editing the entity set names in the designer. When using Code First, it can be done by changing the name of the DbSet property:

    public DbSet<Länder> Lander { get; set; }

or by overriding OnModelCreating in the context:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Länder>().HasEntitySetName("Lander");
    }


It looks like a bug in DbContext API because internally the query is converted to entity SQL which is parsed and if identifier contains national / unicode characters it must be escaped [Länder] but it is not. That is something you probably cannot control from your code.

I'm strongly against using non English names for identifiers (both code and database) so this is just another reason why it prove to be a wrong idea.


This bug is fixed in 4.3 http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-released.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜