В рамках изучения подхода Code-First состряпал небольшой учебный проект. После первой компиляции этого кода в папке %USERPROFILE% создается файл базы данных и все работает отлично. Но если базу данных удалить, то повторно она не создается. Выдается ошибка при компиляции:
An exception occurred while initializing the database. See the InnerException for details.
The underlying provider failed on Open
Cannot open database "WindowsFormsApplication1.BudgetContext" requested by the login. The login failed. Login failed for user 'COMPUTER_NAME\user'.
Помогите разобраться, почему база данных не создается повторно?
Полный код программы
using System.Collections.Generic;
using System.Data.Entity;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
using (var db = new BudgetContext())
{
var goodsList = new GoodsList { Name = "Test Goods List" };
db.GoodsList.Add(goodsList);
db.SaveChanges();
}
}
}
public class BudgetContext : DbContext
{
public DbSet<Goods> Goods { get; set; }
public DbSet<GoodsList> GoodsList { get; set; }
}
public class Goods
{
public int GoodsID { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public int barcode { get; set; }
public int GoodsListID { get; set; }
public virtual GoodsList GoodsList { get; set; }
}
public class GoodsList
{
public int GoodsListID { get; set; }
public string Name { get; set; }
public virtual List<Goods> Goods { get; set; }
}
}
UPD:
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=1934e089b77a5c56" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
UPD2:
Информация о среде:
OS Windows 10 ([Version 10.0.10240]),
Microsoft Visual Studio Community 2015 Version 14.0.23107.0 D14REL
Microsoft .NET Framework Version 4.6.00079
Entity Framework 6.1.3
update-database
создаст бд в случае её отсутствияConnectionString
явно с указанием пути, и имени создаваемой бд. в приведенном ВамиApp.config
я не вижу строки соединенияCreateDatabaseIfNotExists
. и так же у меня создается секция в config следующего вида<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=|DataDirectory|\db.sdf;" providerName="System.Data.SqlServerCe.4.0" /> </connectionStrings>
почему её нет у Вас странно