Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC上添加控制器,不同的对象类型_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 4_Entity Framework 5 - Fatal编程技术网

C# 在MVC上添加控制器,不同的对象类型

C# 在MVC上添加控制器,不同的对象类型,c#,asp.net-mvc,entity-framework,asp.net-mvc-4,entity-framework-5,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Entity Framework 5,所以,我是MVC的新手,刚刚开始学习一些关于DotNetCurry的教程,更具体地说,我面临的是 但答案对我来说并不适用。下面是我的测试项目的上下文 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; namespace Sysbat.Models { public class SysbatContext : DbCo

所以,我是MVC的新手,刚刚开始学习一些关于DotNetCurry的教程,更具体地说,我面临的是

但答案对我来说并不适用。下面是我的测试项目的上下文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace Sysbat.Models
{
    public class SysbatContext : DbContext
    {
        public DbSet<Objeto> Objetos { get; set; }
        public DbSet<Propiedad> Propeidades { get; set; }
        public DbSet<ObjetoPropiedades> ObjetosPropiedades { get; set; }
        public DbSet<ObjetoValores> ObjetosValores { get; set; }
        public DbSet<PropiedadValores> PorpiedadesValores { get; set; }

        public SysbatContext() : base("DevConn"){}
    }
}
这是我试图创建控件的“Objeto”类

/// <summary>
/// Class that hold the Objetos to be used in the system
/// </summary>
public class Objeto
{
    [Key]
    public int Oid { get; set; }
    public string Nombre { get; set; }
    public DateTime FechaCreacion { get; set; }
}
//
///类,该类保存要在系统中使用的对象
/// 
公共类对象
{
[关键]
公共整数Oid{get;set;}
公共字符串Nombre{get;set;}
公共日期时间fechacreation{get;set;}
}

这可能是许多不同的事情。这里确实没有足够的信息来正确诊断

首先,我发现控制器模板非常挑剔。有时,如果您没有重建解决方案的某一部分,它将失败。有时它会无缘无故地失败。而且,它几乎只直接用于实体类和
DbContext
。如果您尝试使用视图模型,或者您将使用存储库或服务之类的东西,而不是直接使用
DbContext
,那么它将失败

长话短说,当你继续使用MVC时,你会发现它的麻烦远远超过它的价值。我仍然使用“添加>控制器…”,但我只是选择“MVC5控制器-空”,然后自己设置所有内容


但是,如果您仍然需要完整的控制器操作和视图生成功能,我建议您首先清理并重建解决方案。如果仍然出现错误,请关闭Visual Studio complete,然后重新打开解决方案。有时这足以让它发挥作用。此外,请确保解决方案中的所有项目都使用完全相同的Entity Framework和MVC版本。如果相互引用的项目之间存在版本不匹配,有时会出现奇怪的行为。

因此,我找到了上一个问题的答案,现在我可以添加控制器了

编辑:

正如所建议的,我正在添加我在链接中找到的解决方案

最后,问题出在我的连接字符串上。在我的web.config中指定了一个提供程序,我没有使用它,所以我只需要删除该设置,它就可以正常工作了

之前:

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>

之后


来自实体框架和WPF的人的不知情想法;entityframework的上下文概念是连接到DB和状态信息。UI数据绑定的概念是,上下文是控件数据绑定到的对象。它们通常不是一回事;模型层必须在它们之间进行转换。所以你不能把一个扔给另一个。
<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>
<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>