efcore.pg: Type reloading doesn't occur when creating database in async mode
Hello,
this is my moved issue from the EntityFrameworkCore repository about using spatial data with Postgresql. https://github.com/aspnet/EntityFrameworkCore/issues/12762
In order to locate the issue I created a single file which contains all the relevant code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NetTopologySuite.Geometries;
using Xunit;
namespace EF.Demo.Test
{
public class GeometryTest
{
public class MyDbContext : DbContext
{
public DbSet<City> Cities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseNpgsql("Host=localhost;Database=test;Username=postgres;Password=mypassword",
o => o.UseNetTopologySuite());
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.HasPostgresExtension("postgis");
}
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public Point Location { get; set; }
}
[Fact]
public async Task Test()
{
var loc = new Point(4.15, 51.51, 0) {SRID = 4326};
using (var ctx = new MyDbContext())
{
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
var city = new City()
{
Id = 1,
Name = "MyCity",
Location = loc
};
await ctx.Cities.AddAsync(city);
await ctx.SaveChangesAsync();
}
using (var ctx = new MyDbContext())
{
var city = await ctx.Cities.FindAsync(1);
Assert.NotNull(city?.Location);
Assert.True(loc.Distance(city.Location) < 0.000001);
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netcoreapp2.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.1.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="2.1.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EF.Demo\EF.Demo.csproj" />
</ItemGroup>
</Project>
I receive the following error:
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
---- Npgsql.NpgsqlException : The NpgsqlDbType 'Geometry' isn't present in your database. You may need to install an extension or upgrade to a newer version.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (9 by maintainers)
Commits related to this issue
- Fix type reloading in database creator async path We've been making changes in the sync path of CreateTables() without doing the same in the async path... Fixes #553 (cherry picked from commit 8714... — committed to npgsql/efcore.pg by roji 6 years ago
- Fix type reloading in database creator async path We've been making changes in the sync path of CreateTables() without doing the same in the async path... Fixes #553 (cherry picked from commit 8714... — committed to npgsql/efcore.pg by roji 6 years ago
I appear to be having the same problem in Npgsql assembly version 5.0.3.0. In my case, my app will auto-migrate the DB on startup, which may create the database. The workaround of calling ReloadTypes() is working:
Creating a new DbContext would probably also have fixed it.
@Andrioden the fix hasn’t been released yet, so it makes sense for this error to still exist. We should try and get 2.1.4 out ASAP.
This is most likely an issue with reloading the types after installing the extension… The test above deletes and recreates the database on every single run. Now, when the extension is first installed into a database (
CREATE EXTENSION postgis), it creates new types but Npgsql has already loaded and cached the types (without the PostGIS types).NpgsqlConnection.ReloadTypes()needs to be called to reload everything, but the EF Core provider is actually supposed to do that when applying migrations that touch extensions.A way to verify whether this is the case, is simply to run the test a second time but comment out the database delete/recreate lines…
@faryu Could you also post the output of the following?
If you’re using
psql,\dxwill work too.