0

User と UserProfile の 2 つのエンティティがあります。User の PK は UserId で、UserProfile の PK は UserProfileId です。アプリで新しいユーザーが作成されるたびに、ユーザーの PK と同じ PK を持つ新しい UserProfile を作成します。次に、UserProfile のプロパティを更新しようとすると、多重度エラーまたはスキーマ無効エラーが発生します。ここに私の2つのエンティティがあります:

public class User
{
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? PhoneExtension { get; set; }
    public string Comment { get; set; }
    public Boolean IsApproved { get; set; }
    public int PasswordFailuresSinceLastSuccess { get; set; }
    public DateTime? LastPasswordFailureDate { get; set; }
    public DateTime? LastActivityDate { get; set; }
    public DateTime? LastLockoutDate { get; set; }
    public DateTime? LastLoginDate { get; set; }
    public string ConfirmationToken { get; set; }
    public DateTime? CreateDate { get; set; }
    public Boolean IsLockedOut { get; set; }
    public DateTime? LastPasswordChangedDate { get; set; }
    public string PasswordVerificationToken { get; set; }
    public DateTime? PasswordVerificationTokenExpirationDate { get; set; }

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


public class UserProfile
{
    public Guid UserProfileId { get; set; } 
    public virtual User ProfileOwner { get; set; }
    public Int64? HomePhone { get; set; }
    public Int64? MobilePhone { get; set; }

    public virtual User Manager { get; set; }
}

..Fluent API を使用して定義した唯一の関係を次に示します。

modelBuilder.Entity<UserProfile>()
        .HasKey(e => e.UserProfileId);
modelBuilder.Entity<UserProfile>()
        .Property(e => e.UserProfileId)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<UserProfile>()
        .HasRequired(e => e.ProfileOwner)
        .WithRequiredDependent(r => r.UserProfile);

最後に、私の UserService は新しいユーザーを作成し、同時に Guid UserProfileId がユーザーの Guid UserId と同じである新しい UserProfile を作成します。ユーザーとプロファイルが作成された直後に、これを使用して UserProfileService で UserProfile のマネージャーを更新しようとします。

public void UpdateUserProfile(UserProfile updatedUserProfile)
{
    UserProfile oldUserProfile = GetUserProfileByID(updatedUserProfile.UserProfileId);

    oldUserProfile.Manager = updatedUserProfile.Manager;
    oldUserProfile.HomePhone = updatedUserProfile.HomePhone;
    oldUserProfile.MobilePhone = updatedUserProfile.MobilePhone;

    this.SetEntityState(oldUserProfile, EntityState.Modified);
    this.UnitOfWork.SaveChanges();
}

this.SetEntityState 行は次のエラーをスローします。

多重度の制約に違反しています。関係「WhelenPortal.Data.Context.UserProfile_ProfileOwner」のロール「UserProfile_ProfileOwner_Source」の多重度は 1 または 0..1 です。

私はこれを2日間機能させようとしています。助けてください!!! 前もって感謝します。

リクエストに応じて、ここにいくつかの追加情報があります。ここでは、リポジトリ パターンと作業単位を使用しています。私の GetUserProfileById コードは以下のとおりです。サービスはリポジトリを使用するため、両方を示します。

public UserProfile GetUserProfileByID(Guid id)
{
    if (id == null)
        throw new BusinessServicesException(Resources.UnableToRetrieveUserProfileExceptionMessage, new ArgumentNullException("id"));

    try
    {
        Model.UserProfile userProfile = _userProfileRepository.GetUserProfileByID(id);

        if (userProfile != null)
            return ToServicesUserProfile(userProfile);

        return null;
    }
    catch (InvalidOperationException ex)
    {
        throw new BusinessServicesException(Resources.UnableToRetrieveUserProfileExceptionMessage, ex);
    }
}

..そしてリポジトリ:

public UserProfile GetUserProfileByID(Guid id)
{
    return this.GetDbSet<UserProfile>().Find(id);
}
4

1 に答える 1

0

だから、これをたくさん遊んだ後、私にとってはうまくいったので、他の誰かを何らかの形で助けることができれば幸いです。私の User クラスはまったく同じままでしたが、私の UserProfile クラスは次のように変更されました。

public class UserProfile
{
    public Guid UserProfileId { get; set; }
    public virtual User ProfileOwner { get; set; }

    public Guid? ManagerId { get; set; }
    public virtual User Manager { get; set; }

    public Int64? HomePhone { get; set; }
    public Int64? MobilePhone { get; set; }
}

そして、流暢なマッピングは次のとおりです。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<User>()
        .HasOptional(u => u.UserProfile)
        .WithRequired(u => u.ProfileOwner);

    modelBuilder.Entity<UserProfile>()
        .HasOptional(u => u.Manager)
        .WithMany()
        .HasForeignKey(u => u.ManagerId);
}
于 2012-07-23T12:53:02.937 に答える