初始化数据库时发生异常.删除后重新创建的用户登录失败 [英] Exception occurred while initializing the database. Login failed for user on re-creation after deletion

查看:81
本文介绍了初始化数据库时发生异常.删除后重新创建的用户登录失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第一次运行时,我的程序在用户文件夹中正确创建了数据库和日志文件.

On the first run my program creates the database and log file correctly in the user's folder.

如果我随后删除数据库和日志文件并再次运行该程序,则会收到错误消息.

If I then delete the database and log file and run the program again I get an error.

using System;
using blocks6.Module.BusinessObjects;

namespace BlocksConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                AddToTable();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        private static void AddToTable()
        {
            using (var db = new blocks6DbContext())
            {
                var inf = new ModuleInfo();
                db.ModulesInfo.Add(inf);
                db.SaveChanges();
            }
            Console.WriteLine("Fin");
        }
    }
}

using System.Data.Entity;
namespace blocks6.Module.BusinessObjects
{
    public class blocks6DbContext : DbContext
    {
        public blocks6DbContext()
            : base("name=ConnectionString")
        {
          //  Database.SetInitializer(new DbInitializer());  uncommenting makes no difference
        }
        public DbSet<ModuleInfo> ModulesInfo { get; set; }
    }

    public class DbInitializer : CreateDatabaseIfNotExists<blocks6DbContext>
    {
    }

    public class ModuleInfo
    {
        public int Id { get; set; }
    }
}

app.config是

app.config is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
<connectionStrings>
  <add name="ConnectionString" connectionString="Integrated Security=SSPI;MultipleActiveResultSets=True;Data Source=(localdb)\mssqllocaldb;Initial Catalog=blocksConsole" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

项目文件为

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{477EDA75-B7F0-4D45-866F-0AB92CE38783}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>BlocksConsole</RootNamespace>
    <AssemblyName>BlocksConsole</AssemblyName>
    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="BlocksDbContext.cs" />
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="EntityFramework">
      <Version>6.2.0</Version>
    </PackageReference>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

我的Win10操作系统是版本1809 Build 17763.194

My Win10 OS is Version 1809 Build 17763.194

重新启动没有帮助.

[更新]

我在运行VS 15.8.1的另一台计算机上创建了一个新的控制台程序 程序锁定在行上

I created a new console program on a different computer running VS 15.8.1 The program locks up on the line

db.ModulesInfo.Add(inf);

当我升级到15.9.4时,锁定停止了.但是,重新创建数据库的错误也会在此计算机上重复出现.

When I upgraded to 15.9.4 the lock up ceased. However the error re-creating the database repeats on this computer too.

第二台计算机上的Winver显示1803 17134.471

Winver on the second computer shows 1803 17134.471

清空回收站没有帮助.

推荐答案

事实证明,已删除的数据库仍显示为SQL Server对象资源管理器(在Visual Studio中)的一个节点

It turns out that the deleted database still shows as a node in SQL Server Object Explorer ( inside Visual Studio)

如果我删除它

然后程序成功运行以重新创建数据库.

then the program runs successfully to re-create the database.

这篇关于初始化数据库时发生异常.删除后重新创建的用户登录失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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