实体框架 Code First MySql 复数表

发布于 2024-12-06 15:37:01 字数 604 浏览 0 评论 0原文

我使用 Microsoft Entity Framework 和代码优先来管理我的数据(使用 MySQL)。我已经定义了一个 POCO 对象,但是,当我尝试添加数据时,它说表 Users 不存在。我查看了数据库,它创建了用户表而不是用户表。我该如何补救?这让我发疯!

谢谢!

  public class User
 {
    [Key,Required]
    public int UserId { get; set; }

    [StringLength(20), Required]
    public string UserName { get; set; }

    [StringLength(30), Required]
    public string Password { get; set; }

    [StringLength(100), Required]
    public string EmailAddress { get; set; }

    [Required]
    public DateTime CreateDate { get; set; }

    [Required]
    public bool IsActive { get; set; }
}

I am using the Microsoft Entity Framework with code first to manage my data (with MySQL). I have defined a POCO object, however, when I try to add data it says table Users doesn't exist. I looked in the DB and it created table User not Users. How can I remedy this? It is driving me nuts!

Thanks!

  public class User
 {
    [Key,Required]
    public int UserId { get; set; }

    [StringLength(20), Required]
    public string UserName { get; set; }

    [StringLength(30), Required]
    public string Password { get; set; }

    [StringLength(100), Required]
    public string EmailAddress { get; set; }

    [Required]
    public DateTime CreateDate { get; set; }

    [Required]
    public bool IsActive { get; set; }
}

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

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

发布评论

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

评论(3

眉黛浅 2024-12-13 15:37:01
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace YourNamespace
{
    public class DataContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
        {
            dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<User> User { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace YourNamespace
{
    public class DataContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
        {
            dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<User> User { get; set; }
    }
}
榕城若虚 2024-12-13 15:37:01

我还没有将 MySQL 与 EF 一起使用,但无论如何我认为解决方案是公正的。您需要关闭复数表约定。

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;

public class MyDbContext: DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {    
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

现在 EF 将在表名称中查找对象名称的文字。

一些精彩的视频教程位于 http://msdn.microsoft.com/en-us/data继续学习实体框架下的/aa937723。为了获得额外的学习体验,您可以不指定上述内容,而是将对象“user”显式映射到表“user”。

附加参考资料:
http:// /blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5- Fluent-api-samples.aspx

I have not used MySQL with EF yet but regardless I think the solution is unbias. You need to turn off Pluralize Table Convention.

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;

public class MyDbContext: DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {    
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

Now EF will look for the literal of your object name to the table name.

Some great video tutorials are at http://msdn.microsoft.com/en-us/data/aa937723 under the Continue Learning Entity Framework. For additional learning experience, you can not specify the above but rather explicitly map the object 'user' to the table 'user'.

Additional References:
http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx

七颜 2024-12-13 15:37:01

您可以在类上放置一个属性来告诉表的名称:

[Table("Users")]
public class User
{
    //...
}

...或者您可以使用 Fluent API。为此,您将重写 DbContext 类中的 OnModelCreating 方法。

public class MyDbContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().Map(t => t.ToTable("Users"));
    }
}

在 EF 的未来版本中,我们承诺能够编写自己的约定。从 4.1 版开始,还没有这个功能...

(还没有用 MySQL 尝试过...)

You can put an attribute on the class telling the name of the table:

[Table("Users")]
public class User
{
    //...
}

...or you could use the fluent API. To do this you will override the OnModelCreating method in your DbContext class.

public class MyDbContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().Map(t => t.ToTable("Users"));
    }
}

In future versions of EF, we've been promised the ability to write our own conventions. Thats not in there yet as of version 4.1...

(Haven't tried it with MySQL...)

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