Exception occurred while initializing the database

2019-07-25 18:09发布

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

and

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

The project file is

<?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>

My Win10 OS is Version 1809 Build 17763.194

Rebooting does not help.

[Update]

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

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

Winver on the second computer shows 1803 17134.471

Emptying the recycle bin does not help.

1条回答
▲ chillily
2楼-- · 2019-07-25 18:36

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

shows in SQL Server Object Explorer

If I delete it

delete the database

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

查看更多
登录 后发表回答