asp.net mvc3 和 Entity Framework 联合使用时发现的问题

wyumening 2013-03-23 10:14:22
问题是在新建Controller类时发现的,新建Controller时选择了新增Data Context Class,将名称命名为
MusicStoreDB,然后生成的代码如下所示:
public class MusicStoreDB : DbContext
{
public DbSet<Album> Album { get; set; }

public DbSet<Genre> Genre { get; set; }

public DbSet<Artist> Artist { get; set; }
}


奇怪的是生成的实体集合的名称像Album,Genre都是单数形式,为什么不是复数形式?我在项目中定义了Album,Genre,Artist三个实体类,放在三个cs文件中,具体代码如下所示:
Album.cs

public class Album
{

public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }


}


Genre.cs

public class Genre
{
public virtual int GenreId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual List<Album> Albums { get; set; }
}


Artist.cs

public class Artist
{
public virtual int ArtistId { get; set; }
public virtual string Name { get; set; }
}


是不是要在哪里配置下?还是说要通过运行EDM向导才能重新配置?
...全文
169 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaotomo 2013-03-25
  • 打赏
  • 举报
回复
楼主看的是《MV3高级编程》这本书里的例子吧。。我也正在做这个。。
wyumening 2013-03-25
  • 打赏
  • 举报
回复
引用 7 楼 yaotomo 的回复:
楼主看的是《MV3高级编程》这本书里的例子吧。。我也正在做这个。。
有没碰到过跟我一样的问题?如果有,是怎么解决的
wyumening 2013-03-24
  • 打赏
  • 举报
回复
引用 5 楼 Mackz 的回复:
这个没啥好纠结的吧,就是个变量名,自己改一下就是了,连BUG都算不上。不过我试了一下倒是带复数形式的,可能是版本问题,我的是EF 5.0.
我用的EF版本是4.1,奇怪的是在控制器中自动生成的代码引用的属性确是复数,所以报错了,这是自动生成的Create操作方法中的代码:
  public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
            
            return View();
        } 
我的Ablum类的代码如下:
public class Album
    {
        
        public virtual int AlbumId { get; set; }
        public virtual int GenreId { get; set; }
        public virtual int ArtistId { get; set; }
        public virtual string Title { get; set; }
        public virtual decimal Price { get; set; }
        public virtual string AlbumArtUrl { get; set; }
        public virtual Genre Genre { get; set; }
        public virtual Artist Artist { get; set; }

                                        
    }
菜牛 2013-03-24
  • 打赏
  • 举报
回复
这个没啥好纠结的吧,就是个变量名,自己改一下就是了,连BUG都算不上。不过我试了一下倒是带复数形式的,可能是版本问题,我的是EF 5.0.
wyumening 2013-03-23
  • 打赏
  • 举报
回复
引用 1 楼 fangxinggood 的回复:
这个在从数据库生成实体的时候,有个Checkbox 问是否要复数的选框。
我只定义了三个实体类,并没有新建数据库,它应该会自己创建一个sqlserver express版本的数据库,现在的问题是生成的实体集合的名称像Album,Genre都是单数形式,并不是复数形式,除此之外没有出现什么其他的问题,我所做的事按照顺序来说是:新建Controller->指定 Data Context类的名称->然后发现生成的实体集合的名称都是单数形式,我并没有通过EDM向导来创建实体数据模型所以不明白应该在哪里配置才好,难道只能重新来过,运行EDM向导在那里配置?
wyumening 2013-03-23
  • 打赏
  • 举报
回复
[
引用 2 楼 caozhy 的回复:
你是在做那个MvcMusicStore么?好好看看人家提供的教程和源代码,你有什么差错。
我现在看的是asp.net mvc3高级编程这本书,它也是在用MvcMusicStore这个项目作为讲解的,我只定义了三个实体类,并没有新建数据库,它应该会自己创建一个sqlserver express版本的数据库,现在的问题是生成的实体集合的名称像Album,Genre都是单数形式,并不是复数形式,除此之外没有出现什么其他的问题,我所做的事按照顺序来说是:新建Controller->指定 Data Context类的名称->然后发现生成的实体集合的名称都是单数形式,我并没有通过EDM向导来创建实体数据模型所以不明白应该在哪里配置才好,难道只能重新来过,运行EDM向导在那里配置?
threenewbee 2013-03-23
  • 打赏
  • 举报
回复
你是在做那个MvcMusicStore么?好好看看人家提供的教程和源代码,你有什么差错。
机器人 2013-03-23
  • 打赏
  • 举报
回复
这个在从数据库生成实体的时候,有个Checkbox 问是否要复数的选框。
ASP.NET MVC with Entity Framework and CSS by Lee Naylor 2016 | ISBN: 1484221362 | English | 608 pages | True PDF | 30 MB This book will teach readers how to build and deploy a fully working example retail website using Microsoft ASP.NET MVC and Entity Framework technologies and recommendations. This book contains several solutions to real world issues that developers will frequently encounter. Whether you are a novice developer or an experienced .NET developer wishing to learn more about MVC and Entity Framework, author Lee Naylor will teach you how to develop a detailed database driven example website using Microsoft ASP.NET and Entity Framework Code First with fully explained code examples including: • Filtering and Searching Related Data using Linq • Using code first migrations to automatically update the database plus seeding data from code • How to use Visual Studio Scaffolding views and controllers • Manage CRUD operations including model binding as recommended by Microsoft and dealing with cascading deletions correctly • Input and data validation using Attributes • Sorting and paging through data and routing for friendly URL formats • Authorization and Authentication using Microsoft Identity v2 by making use of Microsoft example code including correct use of redirection after login and registration • Advanced techniques including managing data concurrency conflicts and using two database contexts • Asynchronous database access and paging • Azure deployment using Code First Migrations, remote debugging and viewing/editing your remote data • CSS Styling including animation and media queries What You Will Learn: • Get up and running quickly with ASP.NET MVC and Entity Framework building a complex web site to display and manage several related entities • How to integrate Identity code into a project • Advanced topics including: Asynchronous database access and managing data conflicts • Working with Microsoft Azure including remote debugging and database access • CSS skills including animations and media queries for use with tablet or mobile/cell phone devices Who This Book Is For: People wanting to learn ASP.NET MVC and Entity Framework ranging from novice developers new to the subject through to more experienced ASP.NET web forms developers looking to migrate from web forms to MVC and Entity Framework. The book assumes some programming knowledge such as object-oriented programming concepts and a basic knowledge of C#.
ASP.NET MVC with Entity Framework and CSS Lee Naylor | 2016 | EPUB| ISBN: 1484221362 | 608 pages | This book will teach readers how to build and deploy a fully working example retail website using Microsoft ASP.NET MVC and Entity Framework technologies and recommendations. This book contains several solutions to real world issues that developers will frequently encounter. Whether you are a novice developer or an experienced .NET developer wishing to learn more about MVC and Entity Framework, author Lee Naylor will teach you how to develop a detailed database driven example website using Microsoft ASP.NET and Entity Framework Code First with fully explained code examples including: • Filtering and Searching Related Data using Linq • Using code first migrations to automatically update the database plus seeding data from code • How to use Visual Studio Scaffolding views and controllers • Manage CRUD operations including model binding as recommended by Microsoft and dealing with cascading deletions correctly • Input and data validation using Attributes • Sorting and paging through data and routing for friendly URL formats • Authorization and Authentication using Microsoft Identity v2 by making use of Microsoft example code including correct use of redirection after login and registration • Advanced techniques including managing data concurrency conflicts and using two database contexts • Asynchronous database access and paging • Azure deployment using Code First Migrations, remote debugging and viewing/editing your remote data • CSS Styling including animation and media queries What You Will Learn: • Get up and running quickly with ASP.NET MVC and Entity Framework building a complex web site to display and manage several related entities • How to integrate Identity code into a project • Advanced topics including: Asynchronous database access and managing data conflicts • Working with Microsoft Azure including remote debugging and database access • CSS skills including animations and media queries for use with tablet or mobile/cell phone devices Who This Book Is For: People wanting to learn ASP.NET MVC and Entity Framework ranging from novice developers new to the subject through to more experienced ASP.NET web forms developers looking to migrate from web forms to MVC and Entity Framework. The book assumes some programming knowledge such as object-oriented programming concepts and a basic knowledge of C#.

62,263

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧