MVC 4 and EF6 database first: issue with mapping

2019-08-14 05:36发布

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

2条回答
孤傲高冷的网名
2楼-- · 2019-08-14 05:46

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-08-14 06:00

Consider trying again with EF5 if you can, I experienced similar issues when trying to make EF6 work with MVC4 (I couldn' make scaffolding work either).

Or go all the way up to the latest versions for everything and try MVC5 with EF6 (this seems to work fine)

查看更多
登录 后发表回答