如何使用Fluent API正确映射实体?

时间:2012-07-11 17:49:44

标签: entity-framework foreign-keys code-first fluent

我有两个实体,一个是User和一个UserProfile。用户的PK是UserId,UserProfile的PK是UserProfileId。每次在我的应用程序中创建新用户时,我都会创建一个新的UserProfile,其PK与User中的PK相同。当我尝试在UserProfile上更新属性时,我最终得到多重性错误或架构无效错误。这是我的两个实体:

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创建了一个新用户,同时创建了一个新的UserProfile,其Guid UserProfileId与User的Guid UserId相同。在创建用户和配置文件之后,我尝试使用以下方法使用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。

我一直在尝试让它在两天内工作,请帮助!提前谢谢。

根据要求,这里有一些额外的信息。我在这里使用存储库模式和工作单元。我的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);
}

1 个答案:

答案 0 :(得分: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);
}