Issue with MVC5 entity framework

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

I am using ASP.NET MVC5, Entity Framework 6 in Visual Studio 2013 in my web application. I am trying my model to work but getting error for one reason another. I have tried both Fluent API and just model on its own. I am sure it must be something silly but I am stuck.. need help. Currently I am getting error in mapping and modeling classes .. i have add the controller in which when i debug for var=a1 i get following error

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Test Model class

public class TestModel {     public int testID { get; set; }     public string Title { get; set; } } 

Mapping Class

public class TestMap : EntityTypeConfiguration<TestMap> {     public TestMap()     {         // Primary Key         this.HasKey(t => t.testID);          // Properties         this.Property(t => t.Title)             .IsRequired()             .HasMaxLength(50);          // Table & Column Mappings         this.ToTable("testTable");         this.Property(t => t.testID).HasColumnName("testID");         this.Property(t => t.Title).HasColumnName("Title");     } } 

Context

public class My_Context : DbContext {     public My_Context()         : base("name=My_Context")     {     }      protected override void OnModelCreating(DbModelBuilder modelBuilder)     {          Database.SetInitializer<DORIS_Context>(new CreateDatabaseIfNotExists<DORIS_Context>());         //modelBuilder.Configurations.Add(new TestMap());         }      public DbSet<TestModel> Test { get; set; } } 

Web.config

<connectionStrings>     <add name="My_Context" </connectionStrings> 

controller

 public class TestController : Controller {     //     // GET: /Test/     public ActionResult Index()     {         using (var db = new DORIS_Context())         {             var query = from b in db.Test                         orderby b.testID                         select b;              foreach (var item in query)             {                 var a1 = item.Title;             }         }          return View();     } } 

回答1:

Is there any reason you initialized the DORIS_Context class in your My_Context class? Your DbSet<TestModel> is defined in My_Context class, but in your action method, you're using DORIS_Context to call it.

If it's not necessary, comment out below line

// Database.SetInitializer<DORIS_Context>(new CreateDatabaseIfNotExists<DORIS_Context>()); 

Then, change EntityTypeConfiguration<TestMap> to below:

public class TestMap : EntityTypeConfiguration<TestModel> 

and your Index action should looks like:

public ActionResult Index() {     using (var db = new My_Context())     {        //     }     return View(); } 

Also you probably need to add keyattribute in TestModel like below:

public class TestModel {     [Key]     public int testID { get; set; }     public string Title { get; set; } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!