【问题标题】:multiplicity conflicts with referential constraint in Role多重性与角色中的引用约束冲突
【发布时间】:2015-10-25 11:15:36
【问题描述】:

我收到的错误消息如下 ----- MVCRegistration.Models.Post_UserProfile: : 多重性与关系“Post_UserProfile”中角色“Post_UserProfile_Target”中的引用约束冲突。因为从属角色中的所有属性都不可为空,所以主体角色的多重性必须为“1”。 MVCRegistration.Models.PostComment_UserProfile: : 多重性与关系“PostComment_UserProfile”中角色“PostComment_UserProfile_Target”中的引用约束冲突。由于 Dependent Role 中的所有属性都不可为空,因此 Principal Role 的多重性必须为“1”。 这里,MVCRegistraion 是解决方案文件名。

现在,我有三个这样的课程---

 public class UserProfile
{
    public UserProfile()
    {
        this.PostComments = new HashSet<PostComment>();
        this.Posts = new HashSet<Post>();
    }
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string AvatarExt { get; set; }
    public virtual ICollection<PostComment> PostComments { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
}

post 类是这样的----

    public class Post
    {
    public Post()
    {
        this.PostComments = new HashSet<PostComment>();
    }
    [Key]
    public int PostId { get; set; }
    public string Message { get; set; }
    public int PostedBy { get; set; }
    public System.DateTime PostedDate { get; set; }
    public virtual ICollection<PostComment> PostComments { get; set; }
    public virtual UserProfile UserProfile { get; set; }
  }

PostComment 是----

   public class PostComment
{
    [Key]
    public int CommentId { get; set; }
    public int PostId { get; set; }
    public string Message { get; set; }
    public int CommentedBy { get; set; }
    public System.DateTime CommentedDate { get; set; }
    public virtual Post Post { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}

我已经使用 fluent api 配置了 USerProfile 类和 Post 类之间的一对多关系---

        modelBuilder.Entity<Post>()
                   .HasOptional<UserProfile>(u => u.UserProfile)
                   .WithMany(p => p.Posts)
                   .HasForeignKey(s => s.PostedBy);

USerProfile 和 Post 评论类之间的一对多关系是这样的-----

    modelBuilder.Entity<PostComment>()
                  .HasOptional<UserProfile>(u => u.UserProfile)
                  .WithMany(p => p.PostComments)
                  .HasForeignKey(s => s.CommentedBy);

现在,我很沮丧地弄清楚这里发生了什么。首先在代码中创建名为 PostedBy 的外键非常简单,但实体框架让事情变得更糟。不知道实体框架现在需要什么。

【问题讨论】:

    标签: asp.net-mvc entity-framework ef-code-first one-to-many


    【解决方案1】:

    正如声明所说,在一对多端,即在从属端,您拥有不可为空的属性,并且在您的流利的 api 中,您正试图使您的外键可以为空。

    只需像这样将 Post 类中的外键从不可为空更改为可空--

     public int? PostedBy {get; set;}
    

    在你的评论类中你必须这样做----

      pulic int? CommentedBy {get; set;}
    

    【讨论】:

    • 如果您从可以为空的 Guid (Guid?) 收到此错误怎么办。还有什么可以检查发生了什么?
    【解决方案2】:

    由于 Dependent Role 中的所有属性都不可为空,因此 Principal Role 的复数必须为 '1'

    但是:

    modelBuilder.Entity<PostComment>()
                  .HasOptional<UserProfile>(u => u.UserProfile)
                  .WithMany(p => p.PostComments)
                  .HasForeignKey(s => s.CommentedBy);
    

    因为 CommentedBy 不能为空,这意味着 Post 评论必须有一个 UserProfile。您有两种选择来解决此问题:

    公共整数?评论人 { 得到;放; }

    HasRequired(u => u.UserProfile).

    它只需要匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多