Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVCSCapfolding使DbSet项名称保持复数_Asp.net Mvc_Asp.net Mvc 3_Scaffolding - Fatal编程技术网

Asp.net mvc MVCSCapfolding使DbSet项名称保持复数

Asp.net mvc MVCSCapfolding使DbSet项名称保持复数,asp.net-mvc,asp.net-mvc-3,scaffolding,Asp.net Mvc,Asp.net Mvc 3,Scaffolding,这是我的模型课 using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace ContactFormWithMultipleCheckboxApp.Models { public class Product { public int ProductId { get; set; } [Required, StringLength(50)]

这是我的模型课

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; }

    }
}
它工作并创建我的控制器、视图和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; }
}
公共类ContactFormWithMultipleCheckboxAppContext:DbContext
{
//您可以将自定义代码添加到此文件。更改不会被覆盖。
// 
//如果希望实体框架删除并重新生成数据库
//无论何时更改模型架构,都会自动添加以下内容
//为Global.asax文件中的应用程序启动方法编写代码。
//注意:这将在每次模型更改时销毁并重新创建数据库。
// 
//System.Data.Entity.Database.SetInitializer(新的System.Data.Entity.DropCreateDatabaseIfModelChanges());
公共数据库集消息{get;set;}
}
如您所见,它将名称创建为消息,但在其他位置(如视图和控制器)上使用消息

这里发生了什么?

在0.9.4中,Steve Sanderson写道:

“根据您的反馈,控制器 名称现在默认为复数 (例如,你得到了更多的人 比PersonController更适用于 键入Person,除非您显式地 输入PersonController作为 安装时的控制器名称)

因此,默认情况下(或惯例),除非你告诉它不要,否则它会使你的名字复数。你说你这么做了,但这并没有使你的控制器或视图多元化

我想知道您是否也需要告诉EntityFramework不要多元化。有关这方面的更多细节,请参阅本文

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; }
}