Я хочу создать программу, которую можно использовать только с .exe, поэтому мне нужно было создать базу данных на основе файлов внутри моей программы.Я пытаюсь использовать SQLite с Entity Framework, поэтому я установил следующие классы:
Program.cs
class Program
{
static void Main(string[] args)
{
using (MyContext context = new MyContext())
{
context.Documents.Add(new Document() { Id = 1, CategoryId = 2, Description = "test", Keywords = "test,TEST,", Text = "TEST test Test" });
Console.WriteLine(context.Documents.Single(x => x.Id == 1).Text);
Console.ReadKey();
}
}
}
class MyContext : DbContext
{
public DbSet<Document> Documents { get; set; }
}
class Document
{
public int Id { get; set; }
public int CategoryId { get; set; }
public string Text { get; set; }
public string Keywords { get; set; }
public string Description { get; set; }
}
при запуске кода это исключение вызывает:
Unable to complete operation. The supplied SqlConnection does not specify an initial catalog or AttachDBFileName
Но я правильно указал строку Соединения, насколько я могу судить в своем app.config:
<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=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</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>
<connectionStrings>
<add name="MyContext" connectionString="Data Source=db.sqlite" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Я попытался настроить БД самостоятельно в конструкторе контекста (используя SQLiteConnection
и SQLiteCommand
), но даже если он успешно создает БД и таблицу, EF все равно выдает мне ту же ошибку.Он не создает файл, когда я закомментирую конструктор.
Есть ли способ достичь того, чего я хочу?