Skip to content

Scaffold does not detect existing primary key name #538

Closed

Description

@WarrenFerrell

Trying to switch a database from database first using flyway to code first using EF. Whenever I scaffold the database (with -d or not), the fluent api (or data annotations) do not register the fact that the existing primary keys on the database are structured like '{entity}_pkey' and assume they are the default 'PK_{entity}'. Looks like I'm going to have to manually specify each as a workaround or rename all of the primary keys.

Table Definition

CREATE SCHEMA "TestSchema";
CREATE TABLE "TestSchema".newtable (
	id varchar NOT NULL,
	CONSTRAINT newtable_pk PRIMARY KEY (id)
)
WITH (
	OIDS=FALSE
) ;

Followed up by running

dotnet ef scaffold "{connectionString}" Npgsql.EntityFrameworkCore.PostgreSQL -d
dotnet ef migrations add DummyCreate

Gives following c# definitions

public partial class npgsqlErrorTestContext : DbContext
{
	public npgsqlErrorTestContext(){}

	public npgsqlErrorTestContext(DbContextOptions<npgsqlErrorTestContext> options)
		: base(options){}

	public virtual DbSet<Newtable> Newtable { get; set; }

	protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
	{
		if (!optionsBuilder.IsConfigured)
		{
			optionsBuilder.UseNpgsql("");
		}
	}

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<Newtable>(entity =>
		{
			entity.Property(e => e.Id).ValueGeneratedNever();
		});
	}
}

[Table("newtable", Schema = "TestSchema")]
public partial class Newtable
{
	[Column("id", TypeName = "character varying")]
	public string Id { get; set; }
}

public partial class DummyCreate : Migration
{
	protected override void Up(MigrationBuilder migrationBuilder)
	{
		migrationBuilder.EnsureSchema(
			name: "TestSchema");

		migrationBuilder.CreateTable(
			name: "newtable",
			schema: "TestSchema",
			columns: table => new
			{
				id = table.Column<string>(type: "character varying", nullable: false)
			},
			constraints: table =>
			{
				table.PrimaryKey("PK_newtable", x => x.id); 
			});
	}

	protected override void Down(MigrationBuilder migrationBuilder)
	{
		migrationBuilder.DropTable(
			name: "newtable",
			schema: "TestSchema");
	}
}

Activity

  1. roji commented on Jul 24, 2018

    @roji
    Member

    Can you please post a quick database table definition, along with the C# model that gets scaffolded, explaining exactly what your issue is?

  2. WarrenFerrell commented on Jul 24, 2018

    @WarrenFerrell
    Author

    @roji Updated original comment. I ended up working around the problem by manually adding entity.HasKey(e => e.Id).HasName("{entity}_pkey"); to each fluent api definition.

  3. roji commented on Jul 27, 2018

    @roji
    Member

    @WarrenFerrell thanks for the extra information, it's clear now.

    This actually seems to be an EF Core issue - I've just filed dotnet/efcore#12821. I'm going to close this for now, but will reopen if attention on the Npgsql side is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions