1

EF 4.1で新しいDbContextの方法を試してみようと思いましたが、問題が発生しました。

データベース(SQL Server 2008)から新しいエンティティデータモデルを生成してから、DbContextジェネレーターを使用します。

Länderというテーブルをフェッチしようとすると、例外が発生します。

var db = new MyEntities();  // instantiate DbContext subclass
var countries = db.Länder.ToList();  // boom

EntitySqlException:単純な識別子'Länder'には、基本的なラテン文字のみが含まれている必要があります。UNICODE文字を使用するには、エスケープされた識別子を使用します。

ラテン語の名前を持つ他のテーブルを試してみると、すべて問題ありません。ObjectContextを使用する場合、この問題は発生しません。

テーブル名をエスケープするにはどうすればよいですか?

4

3 に答える 3

2

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.

于 2011-08-20T11:43:37.173 に答える
2

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");
    }
于 2011-08-25T23:10:17.433 に答える
1

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

于 2012-01-16T01:26:49.400 に答える