实体框架 Code First MySql 复数表
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有将 MySQL 与 EF 一起使用,但无论如何我认为解决方案是公正的。您需要关闭复数表约定。
现在 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.
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
您可以在类上放置一个属性来告诉表的名称:
...或者您可以使用 Fluent API。为此,您将重写 DbContext 类中的 OnModelCreating 方法。
在 EF 的未来版本中,我们承诺能够编写自己的约定。从 4.1 版开始,还没有这个功能...
(还没有用 MySQL 尝试过...)
You can put an attribute on the class telling the name of the table:
...or you could use the fluent API. To do this you will override the OnModelCreating method in your DbContext class.
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...)