MVC 4和EF6数据库第一:发行与制图 [英] MVC 4 and EF6 database first: issue with mapping

查看:111
本文介绍了MVC 4和EF6数据库第一:发行与制图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我沿着临ASP.NET MVC 4亚当·弗里曼在VS 2010下(我下载的是MVC 4模板在线)。我以前的.edmx文件的工作,但在第7章,他没有这样做。我建立一个基本的连接字符串,在我的WebUI中的项目在我的控制器和视图都设在我的web.config文件中的SQL Server。另外,我在下面列出我的域名项目中我的领域类。当我运行应用程序的问题就来了。该应用程序是不承认我的数据库(dbo.Request)我的桌子,而是创造基于实体的命名空间我的类名的表(因此它会创建一个CustRequest表),这也创造了一个_Migration_History表。以prevent这种添加我的班表(MyTableName)]以上​​数据注解。我不明白,为什么我不得不添加此数据注解。此外,EF让我添加一个[关键]我上面的主键,这我能理解,因为我没有一个ID属性,但在书中,他没有这样做。我在想,如果我失去了一些东西明显,因为我是pretty新的MVC。任何帮助将是AP preciated。我与EF 6.工作,谢谢。

 命名空间Requestor.Domain.Entities
    {
        [表(请求)]
        公共类CustRequest
        {
            [键]
            公众诠释的requestId {搞定;组; }
            公共字符串RequestByUserCd {搞定;组; }
            公众的DateTime RequestDateTime {搞定;组; }
            公众的DateTime DUEDATE {搞定;组; }
        }
    }    命名空间Requestor.Domain.Abstract
    {
        公共接口ICustRequestRepository
        {
            IQueryable的< CustRequest>请求{搞定; }
        }
    }    命名空间ITRequ​​estHub.Domain.Concrete
    {
        公共类EFDbContext:的DbContext
        {
            公共DbSet< CustRequest>请求{搞定;组; }
        }
    }
    命名空间ITRequ​​estHub.Domain.Concrete
    {
        公共类EFCustRequestRepository:ICustRequestRepository
        {
            私人EFDbContext语境=新EFDbContext(); //检索数据            公众的IQueryable< CustRequest>请求
            {
                {返回context.Request; }
            }
        }
    }


解决方案

您已经运行到EF,其约定的美妙,有时令人沮丧的。奇妙的,当你的时候,你觉得这个框架未经您明确许可执行任务已经意识到,他们简化生活的公约,但令人沮丧的。

首先,在EF6约定的附加信息可以在这里找到:的http:// msdn.microsoft.com/en-gb/data/jj679962.aspx

在你的第一点,据我所知,EF把你的实体,将在您的数据库中创建表的名称的名称。正如您所发现的,你通过表属性,有超过这个控制,但是你也可以控制它的想你的DbContext

中创建借指约定去除表时,你的复数实体名称的愿望

  modelBuilder.Conventions.Remove< PluralizingTableNameConvention>()

在你的第二个问题,我无法想象,你将需要一个连接到您的请求ID字段属性。这里的约定是,如果字段名称包含ID后缀(不区分大小写),那么EF会自动把它作为一个主键,如果该字段的类型为int或一个GUID将被自动设置为自动种子身份列。

I am following along Pro ASP.NET MVC 4 by Adam Freeman on VS 2010 (I downloaded the MVC 4 template online). I have worked with the .edmx file before, but in Chapter 7 he does not do this. I setup a basic connection string with SQL Server in my web.config file within my WebUI project where my controllers and views are located. Also, I listed my Domain classes within my Domain project below. The problem comes when I run the application. The application is not recognizing my table in my database (dbo.Request) and instead is creating a table based on my class name in the Entities namespace (so it creates a CustRequest table) and it also creates a _Migration_History table. To prevent this I add the Data Annotation above my class [Table("MyTableName")]. I could not figure out why I had to add this Data Annotation. Also, EF made me add a [Key] above my primary key, which i can understand because i do not have an ID property, but in the book he did not do this. I was wondering if I was missing something obvious as I am pretty new to MVC. Any help would be appreciated. I am working with EF 6. Thank you.

    namespace Requestor.Domain.Entities
    {
        [Table("Request")]
        public class CustRequest
        {
            [Key]
            public int RequestId { get; set; }
            public string RequestByUserCd { get; set; }
            public DateTime RequestDateTime { get; set; }
            public DateTime DueDate { get; set; }
        }
    }

    namespace Requestor.Domain.Abstract
    {
        public interface ICustRequestRepository
        {
            IQueryable<CustRequest> Request { get; }
        }
    }

    namespace ITRequestHub.Domain.Concrete
    {
        public class EFDbContext : DbContext
        {
            public DbSet<CustRequest> Request { get; set; }
        }
    }


    namespace ITRequestHub.Domain.Concrete
    {
        public class EFCustRequestRepository : ICustRequestRepository
        {
            private EFDbContext context = new EFDbContext(); //retrieves the data

            public IQueryable<CustRequest> Request
            {
                get { return context.Request; }
            }
        }
    }    

解决方案

You've run into the wonderful and sometimes frustrating part of EF, its conventions. Wonderful when you're aware of the conventions as they simplify life, but frustrating when you feel that the framework is performing tasks without your explicit permission.

Firstly, additional information on EF6 conventions can be found here: http://msdn.microsoft.com/en-gb/data/jj679962.aspx

On your first point, as far as I'm aware, EF takes the name of your entity as the name of the table it will create in your DB. As you've discovered, you do have control over this via the "Table" attribute, but you can also control it's desire to want to pluralize your entity names when creating tables by means convention removal within your DbContext

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>()

On your second point, I cannot imagine that you would require a "Key" attribute attached to your "RequestId" field. The convention here is that if the field name contains a suffix of ID (case-insensitive), then EF will automatically include it as a primary key and if the type of the field is either an Int or a Guid it will be automatically set as an auto-seed identity column.

这篇关于MVC 4和EF6数据库第一:发行与制图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆