1

N を N に接続する 2 つのテーブルがあります。

[Table("Backoffice_Roles")]
public class Role
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid RoleId { get; set; }

    public ICollection<User> Users { get; set; }
}


[Table("Backoffice_Users")]
public class User
{
    // Primary key
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UserId { get; set; }

    public ICollection<Role> Roles { get; set; }
}

Backoffice_Rolesこれはすべて正常に機能し、 、Backoffice_Usersおよびの3 つのテーブルが作成されますRoleUsers

RoleUsersに名前を変更する方法はありBackoffice_RoleUsersますか?

移行ファイルでテーブルの名前を手動で変更しようとしましたが、次のエラーが発生します。

System.Data.Entity.Infrastructure.DbUpdateException: リレーションシップの外部キー プロパティを公開しないエンティティの保存中にエラーが発生しました。単一のエンティティを例外のソースとして識別できないため、EntityEntries プロパティは null を返します。保存中の例外の処理は、エンティティ タイプで外部キー プロパティを公開することで簡単に行うことができます。詳細については、InnerException を参照してください。---> System.Data.Entity.Core.UpdateException: エントリの更新中にエラーが発生しました。詳細については、内部例外を参照してください。---> System.Data.SqlClient.SqlException: 無効なオブジェクト名 'dbo.RoleUsers'。

これは、最後のテーブルの名前を手動で変更しない移行です。

public override void Up()
{
    CreateTable(
        "dbo.Backoffice_Users",
        c => new
            {
                UserId = c.Guid(nullable: false, identity: true),
            })
        .PrimaryKey(t => t.UserId);

    CreateTable(
        "dbo.Backoffice_Roles",
        c => new
            {
                RoleId = c.Guid(nullable: false, identity: true),
            })
        .PrimaryKey(t => t.RoleId);

    CreateTable(
        "dbo.RoleUsers",
        c => new
            {
                Role_RoleId = c.Guid(nullable: false),
                User_UserId = c.Guid(nullable: false),
            })
        .PrimaryKey(t => new { t.Role_RoleId, t.User_UserId })
        .ForeignKey("dbo.Backoffice_Roles", t => t.Role_RoleId)
        .ForeignKey("dbo.Backoffice_Users", t => t.User_UserId)
        .Index(t => t.Role_RoleId)
        .Index(t => t.User_UserId);

}
4

1 に答える 1