MvcScaffolding 保留复数 DbSet 项目名称

发布于 2024-11-03 14:16:29 字数 2085 浏览 0 评论 0原文

这是我的模型类;

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 技术交流群。

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

发布评论

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

评论(1

毁梦 2024-11-10 14:16:29

这篇关于 MvcScaffolding 的博客文章 0.9 .4,史蒂夫·桑德森写道:

“根据您的反馈,控制器
名称现在默认为复数形式
(例如,你得到 PeopleController 而不是
比 PersonController 的模型
输入 Person,除非您明确
输入 PersonController 作为
脚手架时的控制器名称)”

所以默认情况下(或约定)它会复数您的名称,除非您告诉它不要这样做。您声明您这样做了,并且它没有复数您的控制器或视图。

我想知道您是否还需要告诉EntityFramework 不要复数。请参阅这篇文章,“如何EntityFramework 中的单一化” 了解更多详细信息。

In this blog post about MvcScaffolding 0.9.4, Steve Sanderson writes:

"Based on your feedback, controller
names are now pluralized by default
(e.g., you get PeopleController rather
than PersonController for a model of
type Person, unless you explicitly
enter PersonController as the
controller name when scaffolding)"

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.

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