使用的EntityFramework错误的tablename [英] EntityFramework using wrong tablename

查看:221
本文介绍了使用的EntityFramework错误的tablename的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是给我,当我试图从我Bieren表中获取数据的EntityCommandExecutionException。

My code is giving me an EntityCommandExecutionException when i'm trying getting data from my Bieren Table.

异常消息说,它无法找到dbo.Biers,因为它是所谓的dbo.Bieren。

The exception message says that it can't find "dbo.Biers" which is quite obvious because it's called "dbo.Bieren".

我可以很容易地通过只重命名数据库表中解决这个问题。 Altough我不喜欢在我的code的错误修复我的数据库。

I can quite easily fix this by just renaming the table in the database. Altough i don't like fixing my database around my code's errors.

我怎样才能使实体框架使用正确的表,而不是改变我的表的名称吗?

How can i make the entity framework use the correct table instead of changing the name of my table?

先谢谢了。

推荐答案

有关数据库,第一种方式,StriplingWarrior很好提及。但是,如果你使用code-第一种方法,你可以 System.ComponentModel.DataAnnotations.TableAttribute 上圆盾实体类为实体类映射到数据库表。

For the database-first approach, StriplingWarrior is mentioned well. But if you use the code-first approach, you could System.ComponentModel.DataAnnotations.TableAttribute on a targe entity class to map a entity class to a database table.

不过,这种方式有点恼人,因为我们通常要定义一个纯粹的实体类。要做到这一点,你可以把它委托给其他类或使用在的DbContext 类overrided OnModelCreating 方法如下

However, this way is slightly annoying, because we commonly want to define a pure entity class. To do this, you could entrust it to other class or use the overrided OnModelCreating method in the DbContext class as the following.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Category>()
        .ToTable("MyCategories");
    }

这篇关于使用的EntityFramework错误的tablename的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆