-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
7.0.11 gives __EFMigrationsHistory already exists
on dbContext.Database.Migrate();
#2878
Comments
Given the only change in 7.0.11 is #2787 and this is related to schema I suspect migrations on a different schema now always default to |
@roji Sorry to tag you, but have you seen this issue? |
Yes, but I'm currently busy with other urgent matters... I'll try to take a look at this as soon as I can. |
As a temporary workaround you can explicitly set the MigrationHistoryTable schema. We do something like this:
It's not very elegant, but it works. |
Thanks @zlepper this works! |
Thanks @zlepper this helped me also in Npgsql Version 6.0.10 (Npgsql.EntityFrameworkCore.PostgreSQL Version 6.0.22). |
I am seeing the same issue with version 8.0.0 Here are the steps to reproduce this issue.
CREATE ROLE mydbusr WITH
LOGIN
NOSUPERUSER
NOCREATEDB
NOCREATEROLE
INHERIT
NOREPLICATION
CONNECTION LIMIT -1
PASSWORD 'password';
CREATE DATABASE "MyDb"
WITH
OWNER = postgres
ENCODING = 'UTF8'
CONNECTION LIMIT = -1
IS_TEMPLATE = False;
CREATE SCHEMA mydbusr AUTHORIZATION mydbusr;
dotnet new classlib -o EfCoreBug
cd EfCoreBug
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 8.0.0
dotnet add package Microsoft.EntityFrameworkCore.Design --version 8.0.0
using Microsoft.EntityFrameworkCore;
namespace EfCoreBug;
public class MyContext : DbContext
{
public DbSet<MyEntity> MyEntities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseNpgsql("Host=localhost;Username=mydbusr;Password=password;Database=MyDb");
}
public class MyEntity
{
public int Id { get; set; }
}
dotnet ef migrations add InitialCreate
dotnet ef database update
using Microsoft.EntityFrameworkCore;
namespace EfCoreBug;
public class MyContext : DbContext
{
public DbSet<MyEntity> MyEntities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseNpgsql("Host=localhost;Username=mydbusr;Password=password;Database=MyDb");
}
public class MyEntity
{
public int Id { get; set; }
+ public string? NewColumn { get; set; }
}
dotnet ef migrations add AddNewColumn
dotnet ef database update You should get the following error output.
|
Thanks, just suffered this issue and added this code snippet to get around the issue. Only affects my databases where public isn't the main schema. |
This comment was marked as resolved.
This comment was marked as resolved.
3 similar comments
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Everyone, please don't post +1 comments, upvote (👍) the top post instead. |
Thanks ! It's works ! But what is function |
Will this be fixed in a future release? |
Version 8.0.4 also have this problem |
tl;dr this should be fixed for 9.0.0-rc.2 - it would be great to have people's help to confirm on the daily builds (9.0.0-rc.2-ci.20240923T214327 and above)! I can see that this would happen in cases where you have SearchPath set to some non-default schema
In other words, the check whether the table exists and the creation of the table look at different schemas, which is the source of this bug. If anyone is running into issues without settings SearchPath, I'd like to know about it because there may be something else going on (@bassem-mf your repro above didn't set the search path, and indeed doesn't cause an error for me). In #3275 (for 9.0), I already made a change (for unrelated reasons) to no longer check for the existence of the table before creating it, but rather to attempt to create it (with Submitted #3294 to do this, for 9.0.0-rc.2. In the meantime, if someone wants to give the daily build a try (9.0.0-rc.2-ci.20240923T214327 and above), that would be great. |
I want to comment that use "Search Path" parameter isn't always possible, for example if you are making a query to a Postgres database through connection pooler PgBouncer, PgBouncer doesn't support that parameter in the connection string. I have an application where search path isn't used. But the bug appeared since the database context uses schema "data". |
Nothing in the provider requires you to use "Search Path", AFAICT.
Then I'm going to need to see a repro. The fact that the context is configured to use a non-default schema shouldn't in iteslf be problematic in any way. |
Hi @roji ! I tested 9.0.0-rc.2-ci.20240923T214327 and the issue seems to be fixed. Thank you so much for that! I was able to use the dotnet-ef tool to apply the first and second migrations. The only thing is that I got a red error in the output when I applied the first "InitialCreate" migration which was a bit alarming. But the command executed successfully and made all the necessary changed to the database. |
For anyone who is interested in testing this daily build, here is how you do it. Add a file named "NuGet.config" to the root of the project or solution. Make sure you get the file name capitalization right or it will not work. Paste the following into the file: <?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="dotnet9" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json" />
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
<add key="npgsql-vnext" value="https://www.myget.org/F/npgsql-vnext/api/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration> Run the following command to get the specific build of the "Npgsql.EntityFrameworkCore.PostgreSQL" package. dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 9.0.0-rc.2-ci.20240923T214327 If you need to use "dotnet-ef" tool to create migrations, run the following command to get the latest release candidate of the Microsoft.EntityFrameworkCore.Design package. dotnet add package Microsoft.EntityFrameworkCore.Design --version 9.0.0-rc.1.24451.1 Run the following command to update the dotnet-ef tool to the latest release candidate version. dotnet tool update -g dotnet-ef --version 9.0.0-rc.1.24451.1 --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json After you are done testing, downgrade the dotnet-ef tool to the latest stable version. dotnet tool uninstall dotnet-ef -g
dotnet tool install dotnet-ef -g |
@bassem-mf thanks testing and confirming!
Yes, that's indeed expected, since the provider basically switched away from "let's check first if the table is there" to "try to use the table and identify errors in case it's not there". I'll try to take a look at removing this, but in the worst case it's a one-time error log, which isn't the end of the world. |
Yeah, that unfortunately doesn't look like it's going to be feasible... |
@roji We are seeing this same issue as part of .NET core upgrade from 6 to 8. Do you have any suggestions? I am working on creating a new git issue and will be referring this issue. |
@vikasillumina see the milestone and this comment above - this has been fixed for 9. |
After updating from
7.0.4
to7.0.11
I get the error:Npgsql.PostgresException: '42P07: relation "__EFMigrationsHistory" already exists'
on runningdbContext.Database.Migrate();
from the program.cs in an winforms (core 7.0.11) application.My connect string looks like this:
"Host=localhost;Database=dev_copy;Username=postgres;Password=postgres; SearchPath = schema_prod"
After downgrading Npgsql back to
7.0.4
it works again as expected.The text was updated successfully, but these errors were encountered: