Skip to content

Commit 6b772fe

Browse files
axelheermsallin
authored andcommitted
#143: Add code-based configuration
1 parent 702d750 commit 6b772fe

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Data.Common;
2+
using System.Data.Entity;
3+
using System.Data.Entity.Core.Common;
4+
using System.Data.Entity.Infrastructure;
5+
using System.Data.SQLite;
6+
using System.Data.SQLite.EF6;
7+
8+
namespace SQLite.CodeFirst.NetCore.Console
9+
{
10+
public class Configuration : DbConfiguration, IDbConnectionFactory
11+
{
12+
public Configuration()
13+
{
14+
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
15+
SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
16+
17+
var providerServices = (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices));
18+
19+
SetProviderServices("System.Data.SQLite", providerServices);
20+
SetProviderServices("System.Data.SQLite.EF6", providerServices);
21+
22+
SetDefaultConnectionFactory(this);
23+
}
24+
25+
public DbConnection CreateConnection(string connectionString)
26+
=> new SQLiteConnection(connectionString);
27+
}
28+
}

SQLite.CodeFirst.NetCore.Console/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private static void StartDemoUseFile()
3838
System.Console.WriteLine("Starting Demo Application (File)");
3939
System.Console.WriteLine(string.Empty);
4040

41-
using (var context = new FootballDbContext("footballDb"))
41+
using (var context = new FootballDbContext(@"data source=.\db\footballDb\footballDb.sqlite;foreign keys=true"))
4242
{
4343
CreateAndSeedDatabase(context);
4444
DisplaySeededData(context);
@@ -144,4 +144,4 @@ private static void PressEnterToExit()
144144
System.Console.ReadLine();
145145
}
146146
}
147-
}
147+
}

SQLite.CodeFirst.NetCore.Console/SQLite.CodeFirst.NetCore.Console.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="System.Data.SQLite" Version="1.0.112" />
9+
<PackageReference Include="System.Data.SQLite" Version="1.0.112.2" />
10+
<PackageReference Include="System.Data.SQLite.EF6" Version="1.0.112.2" />
1011
</ItemGroup>
1112

1213
<ItemGroup>

0 commit comments

Comments
 (0)