I want to create a program that can be used just with the .exe so I needed to create a file based database from inside my program. I'm trying to use SQLite with Entity Framework so I set up following classes:
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; }
}
upon running the code it throws me this exception:
Unable to complete operation. The supplied SqlConnection does not specify an initial catalog or AttachDBFileName
But I specified the Connection string properly as far as I can tell in my 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>
I've tried setting up the DB on my own in the Context Constructor (using SQLiteConnection
and SQLiteCommand
), but even though it creates the db and table successfully EF still gives me the same error. It doesn't create the file when I comment out the constructor.
Is there a way to achieve what I want?