MvcScaffolding 保留复数 DbSet 项目名称
这是我的模型类;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ContactFormWithMultipleCheckboxApp.Models {
public class Product {
public int ProductId { get; set; }
[Required, StringLength(50)]
public string ProductName { get; set; }
public string Description { get; set; }
public virtual ICollection<Message> Messages { get; set; }
}
public class Message {
public int MessageId { get; set; }
public string From { get; set; }
[Required]
//below one is to validate whether the e-mail address is legit or not
[RegularExpression("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\b")]
public string Email { get; set; }
[StringLength(100)]
public string Subject { get; set; }
public string Content { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
}
我已将 MvcScaffolding 作为 nuget 包安装到我的应用程序中。我正在尝试使用以下代码构建一个简单的脚手架;
PM> Scaffold Controller Message
它可以工作并创建我的控制器、视图和 DBContect 类。但我有一个问题。为什么它在 dbcontect 类中复数我的 dbset 项;
public class ContactFormWithMultipleCheckboxAppContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, add the following
// code to the Application_Start method in your Global.asax file.
// Note: this will destroy and re-create your database with every model change.
//
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<ContactFormWithMultipleCheckboxApp.Models.ContactFormWithMultipleCheckboxAppContext>());
public DbSet<ContactFormWithMultipleCheckboxApp.Models.Message> Messages { get; set; }
}
正如您所看到的,它创建名称为 Messages,但它在其他地方(例如视图和控制器)使用 Message。
这里发生了什么?
here is my model classes;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ContactFormWithMultipleCheckboxApp.Models {
public class Product {
public int ProductId { get; set; }
[Required, StringLength(50)]
public string ProductName { get; set; }
public string Description { get; set; }
public virtual ICollection<Message> Messages { get; set; }
}
public class Message {
public int MessageId { get; set; }
public string From { get; set; }
[Required]
//below one is to validate whether the e-mail address is legit or not
[RegularExpression("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\b")]
public string Email { get; set; }
[StringLength(100)]
public string Subject { get; set; }
public string Content { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
}
And I have installed MvcScaffolding as nuget package to my app. I am trying to a simple scaffolding with following code;
PM> Scaffold Controller Message
it works and creates my controllers, views and DBContect class. but I have one problem. why does it pluralize my dbset item inside dbcontect class;
public class ContactFormWithMultipleCheckboxAppContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, add the following
// code to the Application_Start method in your Global.asax file.
// Note: this will destroy and re-create your database with every model change.
//
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<ContactFormWithMultipleCheckboxApp.Models.ContactFormWithMultipleCheckboxAppContext>());
public DbSet<ContactFormWithMultipleCheckboxApp.Models.Message> Messages { get; set; }
}
as you can see it creates the name as Messages but it uses Message on the other places like view and controller.
What is happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 这篇关于 MvcScaffolding 的博客文章 0.9 .4,史蒂夫·桑德森写道:
所以默认情况下(或约定)它会复数您的名称,除非您告诉它不要这样做。您声明您这样做了,并且它没有复数您的控制器或视图。
我想知道您是否还需要告诉EntityFramework 不要复数。请参阅这篇文章,“如何EntityFramework 中的单一化” 了解更多详细信息。
In this blog post about MvcScaffolding 0.9.4, Steve Sanderson writes:
So by default (or convention) it pluralizes your names unless you tell it not to. You stated that you did that, and it didn't pluralize your controller or views.
I wonder if you also need to tell EntityFramework not to pluralize. See this post, "How to singularize in EntityFramework" for more details about that.