1
votes

I'm trying to use Entity Framework Core with an existing database. This database has several tables that don't have primary keys set due to other processes (out side of the scope of the application I'm building) that will break if the primary key is set.

Is it possible to create models of tables without the primary key?

I'm currently using:

  • Microsoft.EntityFrameworkCore -Version 2.0.1
  • Microsoft.EntityFrameworkCore.SqlServer -Version 2.0.1
  • Microsoft.EntityFrameworkCore.SqlServer.Design -Version
  • Microsoft.EntityFrameworkCore.Tools -Version 2.0.1
  • Microsoft.EntityFrameworkCore.Tools.DotNet -Version 2.0.0

I get this error when running the command below "Unable to generate entity type for table '...'. Please see the warning messages."

Command that I'm running:

Scaffold-DbContext "Server=...myservernamehere...;Database=...mydbnamehere...;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

2
I've done this with dapper using bare queries, and since EF Core is capable of doing bare queries, yes, it is possible. See learnentityframeworkcore.com/raw-sqlRobert Harvey
For what it's worth, "is it possible" is not a productive question. Anything is possible in computing given enough time, money and caffeine.Robert Harvey
A primary key just indicates that no duplicate values can exist in a column of a table. When you do not have a Primary key then duplicate values can exist in a column. So yes you can create models without primary keys. The just isn't a one-to-one mapping between the tables.jdweng
@jdweng I get "Unable to generate entity type for table..." when running the command Scaffold-DbContext "Server=...;Database=...;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir ModelshaydnD
@RobertHarvey have you been able to generate entities with EF Core for tables without primary keys?haydnD

2 Answers

1
votes

Scafford will not work without a Primary Key. However, there is a workaround. At least in 2.2.0, I can't say for sure if this will work in older versions.

  • After you Scaffold the DBContext file for the database

  • At least one of your columns has to be unique data

  • You will need to create a model class for the table with all the columns you care about. It would be best to use the same name and data type as the table.

  • Create your DbSet

    public virtual DbSet<Model of table>  { get; set; }
    
  • In your OnModelCreating(ModelBuilder modelBuilder) create the mapping to the table

    modelBuilder.Entity<Model of table>(entity =>
        {
            entity.HasKey(e => e.{Unique column});
            entity.ToTable("Table's Name");
        }
    )
    
1
votes

Another workaround is to create a temporary project with EF Core 3.0 and do the scaffolding, since it is possible to scaffold a database with no primary keys from this version on. Then you can copy the classes to your old project. You might need some minor changes but it will save a lot of work with big tables or if you have many tables without primary keys and you need to keep the EF Core version for the project.