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