Entity Framework 5、Code-First を使用しています。
2 つのドメイン オブジェクト (またはテーブル) があります。1 番目はUserで、2 番目はUserProfileです。1 人のユーザーは 1 つのプロファイルのみを持つことができ、1 つのプロファイルは 1 人のユーザーにのみ属します。それが1対1の関係です。
ここにクラスがあります.... (コードをわかりやすくするために単純化しました。実際にはもっと複雑です)
ユーザー
public class User {
public virtual Int64 UserId { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual String Username{ get; set; }
public virtual String Email { get; set; }
public virtual String Password { get; set; }
}
ユーザープロフィール
public class UserProfile {
public virtual Int64 UserId { get; set; }
public virtual User User { get; set; }
public virtual Int64 Reputation { get; set; }
public virtual String WebsiteUrl { get; set; }
}
ここに地図があります....
ユーザーマップ
public UserMap() {
this.Property(t => t.Email)
.IsRequired()
.HasMaxLength(100);
this.Property(t => t.Password)
.IsRequired()
.HasMaxLength(15);
this.Property(t => t.Username)
.IsRequired()
.HasMaxLength(15);
}
ユーザー プロファイル マップ
public UserProfileMap()
{
this.HasKey(t => t.UserId);
}
これがコンテキストです....
public class TcContext : DbContext {
static TcContext () {
Database.SetInitializer(new TcContextInitializer());
}
public DbSet<User> Users { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Configurations.Add(new UserMap());
modelBuilder.Configurations.Add(new UserProfileMap());
}
}
そして、ここに私のエラーメッセージがあります....
Unable to determine the principal end of an association between the types 'Tc.Domain.UserProfile' and 'Tc.Domain.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
このように、EF は関係を自動的に決定する必要があると思います。しかし、上記のエラーメッセージが表示されます。この問題についてしばらく調査しましたが、私の場合、問題の良い例が見つかりません。
私の間違いはどこですか?または、マップに何らかの追加の関係を定義する必要がありますか?