コードファーストアプローチを介して、データベースサーバーとして.Net Core 2、EF Core、およびMySQLを使用しているアプリケーションに取り組んでいます。
私は2つのテーブルを持っています:
次のように、Userテーブルはユーザー情報を含むメインテーブルであり、Employeeテーブルは列ID_Userを持つ子テーブルです。
public class User : BaseEntity
{
public int ID_User { get; set; }
public string Name { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public virtual ICollection<Employee> Employees{get;set;}
}
public class Employee : Entity
{
public int ID_Employee { get; set; }
public string Name { get; set; }
public int ID_User { get; set; }
public virtual User User { get; set; }
}
上記のマッピングを使用すると、Everythihgは完全に機能し、両方のテーブルに十分なデータがあります。
ここで、Employeeテーブルの列ID_Userをnull可能にします。
この変更を実装するために、モデルに次の変更を加えました。
public class Employee : Entity
{
public int ID_Employee { get; set; }
public string Name { get; set; }
public int? ID_User { get; set; }
public virtual User User { get; set; }
}
およびマッピングファイル内:
builder.HasOne(x => x.User).WithMany(y => y.Employees).HasForeignKey(z => z.ID_User).IsRequired(false);
dotnet efmigrations add empuserコマンドを実行した後、次の移行コードが生成されました。
migrationBuilder.DropForeignKey(
名前: "FK_Employee_User_ID_User"、
テーブル: "従業員");
migrationBuilder.AlterColumn<int>(
name: "ID_User",
table: "Employee",
nullable: true,
oldClrType: typeof(int));
migrationBuilder.AddForeignKey(
name: "FK_Employee_User_ID_User",
table: "Employee",
column: "ID_User",
principalTable: "User",
principalColumn: "ID_User",
onDelete: ReferentialAction.Restrict);
dotnet ef database updateを実行すると、次のエラーが発生します。
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT FK_Employee_User_ID_User' at line 1
助けてください。
ありがとう
SQLを
SQLを生成していません。すでに試しました。 同じエラーが発生します
'dotnet efmigrationsscript'コマンドを実行すると
木、2018年2月8日には、23時16分mguinness [email protected]は書きました:
SQLを生成できますか
https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#generate-a-sql-script ?
これはSQL構文エラーのようですので、いくつかの手がかりが得られるはずです。—
スレッドを作成したため、これを受け取っています。
このメールに直接返信し、GitHubで表示してください
https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/483#issuecomment-364192130 、
またはスレッドをミュートします
https://github.com/notifications/unsubscribe-auth/AIcy3L4ZQKlICMAtRFQJk8RuFTFBNWbfks5tSzLygaJpZM4R98p8
。
「Microsoft」ログを「情報」に切り替えてから移行を実行すると、SQLがログに記録されます。 例(Microsoft:エラーをMicrosoft:情報に変更)
@caleblloyd 、ありがとうございます。この修正を取得しました。
実際、私はこれにMySQLの公式コネクタを使用していたため、問題が発生していました。
後で、 Pomelo.EntityFrameworkCore.MySqlで同じシナリオを
!!! ザボンロックス!!!
最も参考になるコメント
@caleblloyd 、ありがとうございます。この修正を取得しました。
実際、私はこれにMySQLの公式コネクタを使用していたため、問題が発生していました。
後で、 Pomelo.EntityFrameworkCore.MySqlで同じシナリオを
!!! ザボンロックス!!!