3

1 対 1 のマッピングを使用してレガシー ユーザー テーブルを 2 つのエンティティに分割しようとしていますが、データベースが同期していないという移行エラーが発生し続けます。一対一の関係。

これは既存のデータベースです(ただし、移行が将来的に重要になるため、最初にコードを使用しています)が、データベースに変更を加えていません(ただし、1対1のテーブル分割が正確に何を期待するかはわかりません)。これを取得し続けます:

The model backing the 'Context' context has changed since the database was created.     Consider using Code First Migrations to update the database

データベースを (手動または移行経由で) 更新できますが、新しいフィールドが追加されておらず、名前が一致しているため、実際に何が同期されていないのかわかりません。

ベースエンティティ:

public abstract class BaseEntity<T>
{
    [Key]
    public T Id { get; set; }
    public DateTime CreatedOn { get; set; }
}

メンバーシップ モデル:

public class Membership : BaseEntity<Guid>
{
    public string UserName { get; set; }
    public bool Approved { get; set; }
    public bool Locked { get; set; }
    public Profile Profile { get; set; }
}

プロファイル モデル:

public class Profile : BaseEntity<Guid>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Telephone { get; set; }
    public string Extension { get; set; }
    public Membership Membership { get; set; }
}

メンバーシップ マッピング (これには 1 対 1 の定義があります):

public class MembershipMap : EntityTypeConfiguration<Membership>
{
    public MembershipMap()
    {
        //Primary Key
        this.HasKey(t => t.Id);

        //**Relationship Mappings
        this.HasRequired(m => m.Profile)
            .WithRequiredPrincipal(p => p.Membership);

        //Properties & Column mapping
        this.Property(m => m.Id)
            .HasColumnName("PKID")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(m => m.UserName)
            .HasColumnName("Username")
            .HasMaxLength(255);

        this.Property(m => m.Approved)
            .HasColumnName("IsApproved");

        this.Property(m => m.Locked)
            .HasColumnName("IsLocked");

        this.Property(m => m.CreatedOn)
            .HasColumnName("CreationDate");

        this.ToTable("AppUser");
    }
}

プロファイル マッピング:

public class ProfileMap : EntityTypeConfiguration<Profile>
{
    public ProfileMap()
    {
        //Primary Key
        this.HasKey(t => t.Id);

        //Properties & Column mapping
        this.Property(m => m.Id)
            .HasColumnName("PKID")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(m => m.FirstName)
            .HasColumnName("FirstName");

        this.Property(m => m.LastName)
            .HasColumnName("LastName");

        this.Property(m => m.Email)
            .HasColumnName("Email");

        this.Property(m => m.Telephone)
            .HasColumnName("Telephone");

        this.Property(m => m.Extension)
            .HasColumnName("Extension");

        this.ToTable("AppUser");
    }
}

データベース テーブル すべてのフィールドがマップされているわけではないことはわかっていますが、この段階では必要ありません。それは問題ではないでしょうか?

AppUser データベース テーブル

4

1 に答える 1

0

問題はCode First Mappingsではなく、データベースの切り替えといくつかの厄介な移行が原因でした。

移行をリセットするには、フォローアップの質問から答えをここで見ることができます:

Entity Framework 5のコンテキストをリセットして、初期化されたデータベースで動作していると考える - Code First

EvilBHondaへの称賛

于 2013-08-07T16:08:21.997 に答える