Skip to content

Instantly share code, notes, and snippets.

@dfkeenan
Last active January 7, 2017 06:33
Show Gist options
  • Select an option

  • Save dfkeenan/fc6e02c2ac1529da7f49ae360a8e3278 to your computer and use it in GitHub Desktop.

Select an option

Save dfkeenan/fc6e02c2ac1529da7f49ae360a8e3278 to your computer and use it in GitHub Desktop.
Xenko: How-To use Entity Framework + Sqlite (Windows)

How to use Entity Framework & Sqlite with Xenko

This is an example/how-to for using Entity Framework & Sqlite with Xenko

  1. Add EF references to the "project.json" file of the "MyGame.Game" portable library project. See attached.
  2. Restore NuGet packages.
  3. Add Nuget Packages "Microsoft.EntityFrameworkCore.Design" and "Microsoft.EntityFrameworkCore.Sqlite" to the "MyGame.Windows" project. (not sure why this is required, Asset Compiler errors otherwise).
  4. Create your own DbContext similar to example attached. The filename stuff is important.
  5. Create script to load your DbContext. See "MyDatabaseScript.cs".
  6. Add database file as a Raw Asset and mark as root so it is included in build.
  7. Add an entity to scene with a MyDatabaseScript component (or similar).
  8. Set the DatabaseName property of the MyDatabaseScript component to the name of the database Raw Asset.
  9. ?????
  10. PROFIT

NOTE: Note sure if you need references to NuGet package "Microsoft.EntityFrameworkCore.Design" in either solution.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiliconStudio.Core;
using SiliconStudio.Core.IO;
using SiliconStudio.Core.Serialization.Contents;
using SiliconStudio.Xenko.Engine;
namespace MyDatabaseGame
{
[DataContract]
public class MyDatabaseScript : StartupScript
{
/// <summary>
/// Name of Raw Asset
/// </summary>
[DataMember]
public string DatabaseName { get; set; }
[DataMemberIgnore]
public MyDbContext DataContext { get; private set; }
public override void Start()
{
base.Start();
//TODO make sure this works with assets in subfolders...
var filename = DatabaseName + ".sqlite";
//if database file doesn't exist COPY DB to file so SqLite can read it.
//Is there a better way to do this??
if (!VirtualFileSystem.ApplicationLocal.FileExists(filename))
{
using (var newfile = VirtualFileSystem.ApplicationLocal.OpenStream(filename,VirtualFileMode.Create,VirtualFileAccess.Write))
using (var db = Content.OpenAsStream(DatabaseName,StreamFlags.None))
{
db.CopyTo(newfile);
}
}
filename = VirtualFileSystem.ApplicationLocal.GetAbsolutePath(filename);
DataContext = new MyDbContext(filename);
}
public override void Cancel()
{
DataContext?.Dispose();
DataContext = null;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SiliconStudio.Core;
using SiliconStudio.Core.Serialization.Contents;
namespace MyDatabaseGame
{
public class MyDbContext : DbContext
{
private string fileName;
public MyDbContext(string fileName)
{
this.fileName = fileName;
}
public DbSet<Score> Scores { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Filename={fileName}");
}
}
public class Score
{
public int Id { get; set; }
public string UserName { get; set; }
public int Value { get; set; }
}
}
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.1",
"Microsoft.EntityFrameworkCore.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.Sqlite": "1.1.0"
},
"frameworks": {
"netstandard1.4": { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment