Entity Framework Core でテーブル作成時に "The entity type '(レコードオブジェクト)' requires a primary key to be defined." エラーが
発生しテーブルが作成されない現象と対処法を紹介します。
現象
Entity Framework Core でRelationalDatabaseCreatorオブジェクトのCreateTables() メソッドを呼び出して、テーブルを作成すると以下の例外が発生します。
エラーメッセージ
System.InvalidOperationException
HResult=0x80131509
Message=The entity type '(テーブルのレコードオブジェクト)' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
Source=Microsoft.EntityFrameworkCore
再現プログラム例
UI
下図のフォームを作成します。ボタンは[Create Table]のボタンのみ使用します。
コード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFrameworkCoreCreateTableSqlServer
{
public class MyTable1Rec
{
public int id;
public string Name;
public string Category;
public string Code;
public int Price;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace EntityFrameworkCoreCreateTableSqlServer
{
public class MyDbContext : DbContext
{
public DbSet<MyTable1Rec> MyTable1 { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("Data Source=(DBホスト名またはIPアドレス);Initial Catalog=iPentecSandBox3;User ID=(ユーザーID);Password=(パスワード);Encrypt=False");
}
}
}
フォームのコードです。
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
namespace EntityFrameworkCoreCreateTableSqlServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyDbContext mc = new MyDbContext();
RelationalDatabaseCreator rdc = (RelationalDatabaseCreator)mc.Database.GetService<IDatabaseCreator>();
rdc.CreateTables();
textBox1.Text += "テーブルを作成しました。\r\n";
}
}
}
実行結果
プロジェクトを実行します。下図のウィンドウが表示されます。ウィンドウの[Create Table]ボタンをクリックします。
クリックすると、例外が発生し、以下のメッセージが表示されます。
エラーメッセージ
System.InvalidOperationException
HResult=0x80131509
Message=The entity type 'MyTable1Rec' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
Source=Microsoft.EntityFrameworkCore
原因
テーブルのモデルクラスのメンバをプロパティで実装していないため、フィールドがないと判定されているためです。
対処法
MyTable1Rec.csファイルを以下のコードに変更します。
フィールドに対応するメンバをプロパティで実装します。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFrameworkCoreCreateTableSqlServer
{
public class MyTable1Rec
{
public int id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public string Code { get; set; }
public int Price { get; set; }
}
}
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2022-11-26
作成日: 2022-11-26