question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type reloading doesn't occur when creating database in async mode

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
qwertiecommented, Mar 17, 2021

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:

public static void UpgradeOrCreateDatabase()
{
    using (var context = new MyDbContext(...)) {
        context.Database.Migrate();

        #if DEBUG
        if (context.Scenarios.Count() == 0) {
            var scenario = new Scenario { Name = "Example scenario" };
            context.Scenarios.Add(scenario);
            context.SaveChanges(); // assigns an ID to the scenario

            // Workaround for "NpgsqlDbType 'Geometry' isn't present in your database"
            context.Database.OpenConnection();
            var connection = context.Database.GetDbConnection();
            ((Npgsql.NpgsqlConnection)connection).ReloadTypes();

            AddExampleDataTo(scenario.Id, context);
            context.SaveChanges();
        }
        #endif
    }
}

Creating a new DbContext would probably also have fixed it.

1reaction
rojicommented, Apr 29, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous Database call to MySql Database gives error
{"The database type "MySqlDatabase" does not support asynchronous operations. "} Below is the complete stack image of the error..
Read more >
Availability modes for an availability group - SQL Server ...
Learn the three different availability modes for an Always On availability group: asynchronous-commit mode, synchronous-commit mode, ...
Read more >
Using Async, Await, and Task to keep the WinForms UI ...
Using the async/await pattern in WinForms is an easy win, helping prevent one of the most annoying user experiences - a frozen UI....
Read more >
ModifyDBInstance - Amazon Relational Database Service
Specifies whether major version upgrades are allowed. Changing this parameter doesn't result in an outage and the change is asynchronously applied as soon...
Read more >
Write asynchronous DAO queries
One-shot read queries that read data from your database only once and return a result with the snapshot of the database at that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found