3

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
  • test map is commented out and would be required since the model doesnt meet standard conventions. The error effectively says. Model is Broken. Commented Dec 12, 2013 at 11:32

1 Answer 1

5

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.