static async Task <int> Main(string[] args) { System.Console.WriteLine("Hello World!"); var connection = new SqliteConnection(@"DataSource=:memory:"); connection.Open(); connection.EnableExtensions(true); SpatialiteLoader.Load(connection); try { var options = new DbContextOptionsBuilder <PortContext>() .UseSqlite(connection, x => x.UseNetTopologySuite()) .Options; // Create the schema in the database using (var context = new PortContext(options)) { var port = new Port() { Id = Guid.NewGuid() }; await context.AddAsync(port); await context.SaveChangesAsync(); } } finally { connection.Close(); } System.Console.ReadKey(); return(1); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public SqliteRelationalConnection( [NotNull] RelationalConnectionDependencies dependencies, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder) : base(dependencies) { Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder)); _rawSqlCommandBuilder = rawSqlCommandBuilder; var optionsExtension = dependencies.ContextOptions.Extensions.OfType <SqliteOptionsExtension>().FirstOrDefault(); if (optionsExtension != null) { _loadSpatialite = optionsExtension.LoadSpatialite; if (_loadSpatialite) { var relationalOptions = RelationalOptionsExtension.Extract(dependencies.ContextOptions); if (relationalOptions.Connection != null) { // TODO: Provide a better hook to do this for both external connections and ones created by EF SpatialiteLoader.Load((SqliteConnection)relationalOptions.Connection); } } } }
public async Task Test_Crash_for_EF_Team() { var connection = new SqliteConnection(@"DataSource=:memory:"); connection.Open(); connection.EnableExtensions(true); SpatialiteLoader.Load(connection); try { var options = new DbContextOptionsBuilder <PortContext>() .UseSqlite(connection, x => x.UseNetTopologySuite()) .Options; // Create the schema in the database using (var context = new PortContext(options)) { Assert.True(context.Database.EnsureCreated(), "Assert failed, no database created"); var port = new Port() { Id = Guid.NewGuid() }; await context.AddAsync(port); await context.SaveChangesAsync(); } } finally { connection.Close(); } }
private void LoadSpatialite() { if (!_loadSpatialite) { return; } var connection = (TaosConnection)DbConnection; SpatialiteLoader.Load(DbConnection); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> protected override DbConnection CreateDbConnection() { var connection = new SqliteConnection(ConnectionString); if (_loadSpatialite) { SpatialiteLoader.Load(connection); } return(connection); }
private void LoadSpatialite() { if (!_loadSpatialite) { return; } var connection = (SqliteConnection)DbConnection; connection.EnableExtensions(); SpatialiteLoader.Load(DbConnection); connection.EnableExtensions(false); }
private void InitializeDbConnection(DbConnection connection) { if (_loadSpatialite) { SpatialiteLoader.Load(connection); } if (connection is SqliteConnection sqliteConnection) { if (_commandTimeout.HasValue) { sqliteConnection.DefaultTimeout = _commandTimeout.Value; } sqliteConnection.CreateFunction <object, object, object>( "ef_mod", (dividend, divisor) => { if (dividend == null || divisor == null) { return(null); } if (dividend is string s) { return(decimal.Parse(s, CultureInfo.InvariantCulture) % Convert.ToDecimal(divisor, CultureInfo.InvariantCulture)); } return(Convert.ToDouble(dividend, CultureInfo.InvariantCulture) % Convert.ToDouble(divisor, CultureInfo.InvariantCulture)); }); sqliteConnection.CreateFunction( name: "ef_compare", (decimal? left, decimal? right) => left.HasValue && right.HasValue ? decimal.Compare(left.Value, right.Value) : default(int?), isDeterministic: true); } else { _logger.UnexpectedConnectionTypeWarning(connection.GetType()); } }
private void InitializeDbConnection(DbConnection connection) { if (_loadSpatialite) { SpatialiteLoader.Load(connection); } if (connection is SqliteConnection sqliteConnection) { if (_commandTimeout.HasValue) { sqliteConnection.DefaultTimeout = _commandTimeout.Value; } sqliteConnection.CreateFunction <string, string, bool?>( "regexp", (pattern, input) => { if (input == null || pattern == null) { return(null); } return(Regex.IsMatch(input, pattern)); }, isDeterministic: true); sqliteConnection.CreateFunction <object, object, object?>( "ef_mod", (dividend, divisor) => { if (dividend == null || divisor == null) { return(null); } if (dividend is string s) { return(decimal.Parse(s, CultureInfo.InvariantCulture) % Convert.ToDecimal(divisor, CultureInfo.InvariantCulture)); } return(Convert.ToDouble(dividend, CultureInfo.InvariantCulture) % Convert.ToDouble(divisor, CultureInfo.InvariantCulture)); }, isDeterministic: true); sqliteConnection.CreateFunction( name: "ef_add", (decimal? left, decimal? right) => left + right, isDeterministic: true); sqliteConnection.CreateFunction( name: "ef_divide", (decimal? dividend, decimal? divisor) => dividend / divisor, isDeterministic: true); sqliteConnection.CreateFunction( name: "ef_compare", (decimal? left, decimal? right) => left.HasValue && right.HasValue ? decimal.Compare(left.Value, right.Value) : default(int?), isDeterministic: true); sqliteConnection.CreateFunction( name: "ef_multiply", (decimal? left, decimal? right) => left * right, isDeterministic: true); sqliteConnection.CreateFunction( name: "ef_negate", (decimal? m) => - m, isDeterministic: true); } else { _logger.UnexpectedConnectionTypeWarning(connection.GetType()); } }