问题与MVC5实体框架 [英] Issue with MVC5 entity framework

查看:87
本文介绍了问题与MVC5实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的web应用程序使用ASP.NET MVC5,实体框架6在Visual Studio 2013。我想我的模型工作,但得到错误的原因之一另一个。我曾尝试对自己都流利的API和公正的典范。我相信这一定是一些愚蠢的,但我坚持..需要帮助。目前,我在测绘和建模类得到错误..我有加在其中,当我调试var的控制器= A1我得到以下错误

类型'System.Data.Entity.ModelConfiguration.ModelValidationException的异常出现在EntityFramework.dll但在用户code没有处理

测试模型类

 公共类TestModel
{
    公众诠释testID {搞定;组; }
    公共字符串名称{搞定;组; }
}

映射类

 公共类TestMap:EntityTypeConfiguration< TestMap>
{
    公共TestMap()
    {
        // 首要的关键
        this.HasKey(T => t.testID);        //属性
        this.Property(T => t.Title)
            。是必须的()
            .HasMaxLength(50);        //表&安培;列映射
        this.ToTable(TestTable的);
        this.Property(T => t.testID).HasColumnName(testID);
        this.Property(T => t.Title).HasColumnName(标题);
    }
}

上下文

 公共类My_Context:的DbContext
{
    公共My_Context()
        :基地(NAME = My_Context)
    {
    }    保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
    {
         Database.SetInitializer&所述; DORIS_Context>(新CreateDatabaseIfNotExists&所述; DORIS_Context>());
        //modelBuilder.Configurations.Add(new TestMap());
    }    公共DbSet< TestModel>测试{搞定;组; }
}

Web.config文件

 <&是connectionStrings GT;
    <添加名称=My_Context
< /&是connectionStrings GT;

控制器

 公共类的TestController:控制器
{
    //
    // GET:/测试/
    公众的ActionResult指数()
    {
        使用(VAR DB =新DORIS_Context())
        {
            VAR查询从B在db.Test =
                        排序依据b.testID
                        选择B:            的foreach(查询VAR项)
            {
                VAR A1 = item.Title;
            }
        }        返回查看();
    }
}


解决方案

有你在初始化 DORIS_Context 类的任何原因,你的 My_Context 类?你的 DbSet&LT; TestModel&GT; My_Context 类,但在你的操作方法,你使用<$ c定义$ C> DORIS_Context 来调用它。

如果它没有必要,注释掉以下行

  // Database.SetInitializer&LT; D​​ORIS_Context&GT;(新CreateDatabaseIfNotExists&LT; D​​ORIS_Context&GT;());

然后,将 EntityTypeConfiguration&LT; TestMap&GT; 来如下:

 公共类TestMap:EntityTypeConfiguration&LT; TestModel&GT;

和食指的动作应该是这样的:

 公众的ActionResult指数()
{
    使用(VAR DB =新My_Context())
    {
       //
    }
    返回查看();
}

另外你可能需要在TestModel添加keyattribute象下面这样:

 公共类TestModel
{
    [键]
    公众诠释testID {搞定;组; }
    公共字符串名称{搞定;组; }
}

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

解决方案

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

这篇关于问题与MVC5实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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