EF4 CTP5 Code First 方法忽略表属性

发布于 2024-10-08 16:58:56 字数 903 浏览 2 评论 0原文

我正在使用 EF4 CTP5 代码优先方法,但无法使其正常工作。我有一个名为“Company”的类和一个名为“CompanyTable”的数据库表。我想将 Company 类映射到 CompanyTable 表,所以有这样的代码:

[Table(Name = "CompanyTable")]
    public class Company
    {
        [Key]
        [Column(Name = "CompanyIdNumber", DbType = "int")]
        public int CompanyNumber { get; set; }

        [Column(Name = "CompanyName", DbType = "varchar")]
        public string CompanyName { get; set; }
    }

然后我像这样调用它:

var db = new Users();
            var companies = (from c in db.Companies
                            select c).ToList();

但是它出错了:

无效的对象名称'dbo.Companies'。

这显然不尊重类上的 Table 属性,即使它显示 此处 支持 Table 属性。此外,它还对正在搜索的名称进行复数化(Companies 而不是 Company)。如何将类映射到表名称?

I'm using EF4 CTP5 code first approach but am having trouble getting it to work. I have a class called "Company" and a database table called "CompanyTable". I want to map the Company class to the CompanyTable table, so have code like this:

[Table(Name = "CompanyTable")]
    public class Company
    {
        [Key]
        [Column(Name = "CompanyIdNumber", DbType = "int")]
        public int CompanyNumber { get; set; }

        [Column(Name = "CompanyName", DbType = "varchar")]
        public string CompanyName { get; set; }
    }

I then call it like so:

var db = new Users();
            var companies = (from c in db.Companies
                            select c).ToList();

However it errors out:

Invalid object name 'dbo.Companies'.

It's obviously not respecting the Table attribute on the class, even though it says here that Table attribute is supported. Also it's pluralizing the name it's searching for (Companies instead of Company.) How do I map the class to the table name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兲鉂ぱ嘚淚 2024-10-15 16:58:56

在继承自 DbContext 的类上,重写 OnModelCreating 方法

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Company>().ToTable("dbo.CompanyTable");
        }

on you class that inherits from DbContext, override the OnModelCreating method

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Company>().ToTable("dbo.CompanyTable");
        }
萌逼全场 2024-10-15 16:58:56

忘记将对 ctp5 dll 的引用添加到我的架构项目中,它使用 System.Data.Linq.Mapping 代替。

Forgot to add a reference to the ctp5 dll to my schemas project, it was using System.Data.Linq.Mapping instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文