nHibernate does not appear to support foreign keys with SQLite. By default foreign keys are not supported in SQLite either, but may be enabled by running PRAGMA foreign_keys = ON. But I don't know how or where to do this in my nHibernate code.
Any ideas on how to get nHibernate to work with foreign keys in SQLite? I found an old work-around which did not work (Does SQLite coupled with NHibernate support referential integrity / foreign keys?).
Here is my code. The mapping is done with hbm xml files, but that shouldn't matter. The error returned is "SQL logic error or missing database near "constraint": syntax error".
namespace ConsoleApp
{
public class Employee
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
public virtual int DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual List<Employee> Employees { get; set; }
}
class Program
{
static void Main(string[] args)
{
var cfg = new Configuration();
cfg.DataBaseIntegration(x =>
{
x.ConnectionString = "data source=:memory:";
x.Driver<SQLite20Driver>();
x.Dialect<MsSql2012Dialect>();
x.ConnectionReleaseMode = ConnectionReleaseMode.OnClose;
});
cfg.AddAssembly(Assembly.GetExecutingAssembly());
var sessionFactory = cfg.BuildSessionFactory();
var session = sessionFactory.OpenSession();
new SchemaExport(cfg).Execute(true, true, false, session.Connection, null);
}
}
}