Repository navigation
Closed
Description
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 DummyCreateGives 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");
}
}Metadata
Metadata
Assignees
Labels
No labels
Activity
roji commentedon Jul 24, 2018
Can you please post a quick database table definition, along with the C# model that gets scaffolded, explaining exactly what your issue is?
WarrenFerrell commentedon Jul 24, 2018
@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.roji commentedon Jul 27, 2018
@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.