Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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# 问题: 在我的上下文中将名称添加到基: 基本(“名称=连接字符串名称”) 我在连接字符串中犯了一个错误并修复了它_C#_Sql_Asp.net Mvc_Entity Framework 6_Asp.net Mvc Scaffolding - Fatal编程技术网

C# 问题: 在我的上下文中将名称添加到基: 基本(“名称=连接字符串名称”) 我在连接字符串中犯了一个错误并修复了它

C# 问题: 在我的上下文中将名称添加到基: 基本(“名称=连接字符串名称”) 我在连接字符串中犯了一个错误并修复了它,c#,sql,asp.net-mvc,entity-framework-6,asp.net-mvc-scaffolding,C#,Sql,Asp.net Mvc,Entity Framework 6,Asp.net Mvc Scaffolding,使用Visual Studio 2015 升级了mysql服务器,在此过程中,visual Studio的mysql从6.9.7升级到6.9.8 在我的web配置中,仍然有对旧6.9.7版本的引用 以下是解决问题的git diff: - <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,V

使用Visual Studio 2015

升级了mysql服务器,在此过程中,visual Studio的mysql从6.9.7升级到6.9.8

在我的web配置中,仍然有对旧6.9.7版本的引用

以下是解决问题的git diff:

-      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version=6.9.7.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"></provider>
+      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version=6.9.8.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"></provider>
-
+      

我在使用VS 2015时也遇到了这个问题。我在这里尝试了所有其他的解决方案,但都没有成功。事实证明,我的连接字符串(尽管其格式与MS告诉我们的格式完全相同)需要双\才能正常工作

以下是不起作用的情况:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\V11.0;AttachDbFilename=|DataDirectory|\SquashSpiderDB.mdf;Initial Catalog=SquashSpiderDB;Integrated Security=True" providerName="System.Data.SqlClient" />

为了让代码生成器工作,我将其更改为:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\\V11.0;AttachDbFilename=|DataDirectory|\SquashSpiderDB.mdf;Initial Catalog=SquashSpiderDB;Integrated Security=True" providerName="System.Data.SqlClient" />

请注意V11.0前面的双\字符和数据库名称

希望这能为其他人节省一些时间

更新

这将使代码生成正常工作,但应用程序将无法运行,因为\V11.0不是有效的连接字符串。在我看来,MS在解析连接字符串时的代码生成中有一个bug。在运行代码生成后,我不得不将其改回单个\以使应用程序再次工作

更新2

在我的合作伙伴进一步挖掘之后,我们发现真正破坏代码生成的是我们更改了“初始目录”字段。当向导创建项目时,它会自动将初始目录设置为aspnet--。然后,我们将这个初始目录字段更改为DB。这对应用程序的运行非常有效。它可以很好地进入数据库。但由于某些原因,这会破坏代码脚手架的生成。通过将初始目录恢复到以前的状态(aspnet),脚手架重新开始工作(不需要\V11.0)


希望这对将来的人有所帮助。

在使用scaffold和“ASP.NET MVC5使用实体框架视图”创建控制器时,我遇到了同样的问题

问题是因为我在web.config中的
之前提供了
标记。在
解决问题之后设置

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

我猜,在搭建脚手架时,ASP.NET MVC希望先解析实体框架,然后解析连接字符串,因为我之前提供了连接字符串,所以在解析实体框架版本后,它找不到连接字符串,因此引发调用问题。

检查实体之间的关系或其他模型设计为了测试,创建没有关系的新类模型,并使用Scaffold生成控制器和视图。
适用于我。

使用:VS 2015社区版和EF 6.1.3 还实现了:Seed方法,带有一个个性化类,并在web.config文件中配置为在每次模型更改时运行。
这似乎与web.config文件中的一些错误配置有关,在我的案例中,就像我在本文中看到的文件其他部分的一些案例一样,该部分以不同的内容重复,但它当然重复了主标记。该部分不合适的情况,在该部分之上,也是导致相同行为和错误的原因尝试脚手架时缩小消息范围。

这是(我99%肯定)连接字符串问题。在Web.Config中修复该问题后,错误消失了。我在另一个项目中首先使用了DB,然后将DB第一个连接字符串从App.Config复制到Web.Config中(使用相同的名称)它如预期的那样工作。

我意识到这个问题现在已经过时了,但我想我应该发布解决问题的方法,以防以后对任何人都有帮助。在我的情况下,它是其他答案中提到的几件事的组合。为了保护我的连接字符串,我有

  • 已将连接字符串从Web.config文件中移出
  • 已将服务器名称和密码移动到Web.config的AppSettings部分中引用的单独文件中
  • 使用SqlConnectionStringBuilder将元素放在一起,然后将其传递给我的上下文类构造函数
  • 完成此操作后,我无法再创建任何控制器。要解决此问题,我必须

  • 将完整的连接字符串放回Web.config并删除对外部连接字符串文件的引用
  • 向我的上下文类添加一个无参数构造函数,并将其命名为我的connectionString,如下所示:
    public contextClass():base(“name=connectionStringName”){}

  • 重新构建解决方案,然后再次创建控制器,它就成功了


  • 大家好。我知道我有点晚了,但我认为这仍然是有效的分享我的经验与这个问题

    我在两个项目中遇到了这个消息,在这两个项目中,问题都是连接字符串

    在第一种情况下,它是“InitialCatalog”,而不是“InitialCatalog”(分开)

    在第二种情况下,服务器名称(数据源参数)错误

    我希望有帮助


    致以最诚挚的问候。

    出于某种原因,当我在web.config中的
    之间注释oracle.manageddataaccess.client部分时,它成功了。(VS 2015和ODT 12)

    
    
    在我的案例中,问题是由外部应用程序设置文件引起的:

    <appSettings configSource="appSettings.config" />
    
    
    
    将应用程序设置恢复到web.config文件解决了此问题

    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    

    您的上下文类可能正在引发异常

    我一直在关注《C#6.0和.NET4.6框架》一书数据访问层部分的最后一个练习是添加日志记录。嗯,我想这会破坏scaffold向导。可能
    sqllog.txt
    HttpRuntime
    上的文件权限没有定义……谁知道呢。当我注释完所有这些内容后,它又起作用了

    namespace AutoLotDAL.EF
    {
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        using System.Data.Entity.Infrastructure.Interception;
        using AutoLotDAL.Interception;
        using AutoLotDAL.Models;
        using System;
        using System.Data.Entity.Core.Objects;
        using System.Web;
    
        public class AutoLotEntities : DbContext
        {
            //static readonly DatabaseLogger databaseLogger =
            //    new DatabaseLogger($"{HttpRuntime.AppDomainAppPath}/sqllog.txt", true);
    
            public AutoLotEntities()
                : base("name=AutoLotConnection")
            {
                ////DbInterception.Add(new ConsoleWriterInterceptor());
                //databaseLogger.StartLogging();
                //DbInterception.Add(databaseLogger);
    
                //// Interceptor code
                //var context = (this as IObjectContextAdapter).ObjectContext;
                //context.ObjectMaterialized += OnObjectMaterialized;
                //context.SavingChanges += OnSavingChanges;
            }
    
            //void OnObjectMaterialized(object sender,
            //    System.Data.Entity.Core.Objects.ObjectMaterializedEventArgs e)
            //{
    
            //}
    
            //void OnSavingChanges(object sender, EventArgs eventArgs)
            //{
            //    // Sender is of type ObjectContext.  Can get current and original values,
            //    // and cancel/modify the save operation as desired.
            //    var context = sender as ObjectContext;
            //    if (context == null)
            //        return;
            //    foreach (ObjectStateEntry item in
            //        context.ObjectStateManager.GetObjectStateEntries(
            //            EntityState.Modified | EntityState.Added))
            //    {
            //        // Do something important here
            //        if ((item.Entity as Inventory) != null)
            //        {
            //            var entity = (Inventory)item.Entity;
            //            if (entity.Color == "Red")
            //            {
            //                item.RejectPropertyChanges(nameof(entity.Color));
            //            }
            //        }
            //    }
            //}
    
            //protected override void Dispose(bool disposing)
            //{
            //    DbInterception.Remove(databaseLogger);
            //    databaseLogger.StopLogging();
            //    base.Dispose(disposing);
            //}
    
            public virtual DbSet<CreditRisk> CreditRisks { get; set; }
            public virtual DbSet<Customer> Customers { get; set; }
            public virtual DbSet<Inventory> Inventory { get; set; }
            public virtual DbSet<Order> Orders { get; set; }
        }
    }
    
    namespace AutoLotDAL.EF
    {
    
    <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact"/>
    
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    
    -      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version=6.9.7.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"></provider>
    +      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version=6.9.8.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"></provider>
    
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\V11.0;AttachDbFilename=|DataDirectory|\SquashSpiderDB.mdf;Initial Catalog=SquashSpiderDB;Integrated Security=True" providerName="System.Data.SqlClient" />
    
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\\V11.0;AttachDbFilename=|DataDirectory|\SquashSpiderDB.mdf;Initial Catalog=SquashSpiderDB;Integrated Security=True" providerName="System.Data.SqlClient" />
    
    <configSections>   
        <section name="entityFramework" type" />
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <!--<section name="oracle.manageddataaccess.client" />-->
    </configSections>
    
    <appSettings configSource="appSettings.config" />
    
    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    
    namespace AutoLotDAL.EF
    {
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        using System.Data.Entity.Infrastructure.Interception;
        using AutoLotDAL.Interception;
        using AutoLotDAL.Models;
        using System;
        using System.Data.Entity.Core.Objects;
        using System.Web;
    
        public class AutoLotEntities : DbContext
        {
            //static readonly DatabaseLogger databaseLogger =
            //    new DatabaseLogger($"{HttpRuntime.AppDomainAppPath}/sqllog.txt", true);
    
            public AutoLotEntities()
                : base("name=AutoLotConnection")
            {
                ////DbInterception.Add(new ConsoleWriterInterceptor());
                //databaseLogger.StartLogging();
                //DbInterception.Add(databaseLogger);
    
                //// Interceptor code
                //var context = (this as IObjectContextAdapter).ObjectContext;
                //context.ObjectMaterialized += OnObjectMaterialized;
                //context.SavingChanges += OnSavingChanges;
            }
    
            //void OnObjectMaterialized(object sender,
            //    System.Data.Entity.Core.Objects.ObjectMaterializedEventArgs e)
            //{
    
            //}
    
            //void OnSavingChanges(object sender, EventArgs eventArgs)
            //{
            //    // Sender is of type ObjectContext.  Can get current and original values,
            //    // and cancel/modify the save operation as desired.
            //    var context = sender as ObjectContext;
            //    if (context == null)
            //        return;
            //    foreach (ObjectStateEntry item in
            //        context.ObjectStateManager.GetObjectStateEntries(
            //            EntityState.Modified | EntityState.Added))
            //    {
            //        // Do something important here
            //        if ((item.Entity as Inventory) != null)
            //        {
            //            var entity = (Inventory)item.Entity;
            //            if (entity.Color == "Red")
            //            {
            //                item.RejectPropertyChanges(nameof(entity.Color));
            //            }
            //        }
            //    }
            //}
    
            //protected override void Dispose(bool disposing)
            //{
            //    DbInterception.Remove(databaseLogger);
            //    databaseLogger.StopLogging();
            //    base.Dispose(disposing);
            //}
    
            public virtual DbSet<CreditRisk> CreditRisks { get; set; }
            public virtual DbSet<Customer> Customers { get; set; }
            public virtual DbSet<Inventory> Inventory { get; set; }
            public virtual DbSet<Order> Orders { get; set; }
        }
    }
    
    <appSettings configSource="App_Config\Server\AppSettings.config" />
    <connectionStrings configSource="bin\Connections.config" />
    
    <connectionStrings>
        <add name="UIBuilderContext" connectionString="metadata=res:/ ..... " />
      </connectionStrings>