asp.net-mvc - 多重性与角色中的参照约束冲突

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

我收到的错误消息如下 ----- MVCRegistration.Models.Post_UserProfile::多重性与关系“Post_UserProfile”中角色“Post_UserProfile_Target”中的引用约束冲突。由于 Dependent Role 中的所有属性都不可为 null,因此 Principal Role 的多重性必须为“1”。 MVCRegistration.Models.PostComment_UserProfile::多重性与关系“PostComment_UserProfile”中角色“PostComment_UserProfile_Target”中的引用约束冲突。由于 Dependent Role 中的所有属性都不可为 null,因此 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 comment 类之间的一对多关系是这样的-----

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

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

最佳答案

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

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

 public int? PostedBy {get; set;}

再次在您的 Comment 类中,您必须这样做-----

  pulic int? CommentedBy {get; set;}

关于asp.net-mvc - 多重性与角色中的参照约束冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33329043/

相关文章:

c# - 错误 : "Cannot implicitly convert type"

c# - 在 DbUpdateException 中获取触发器的名称或错误信息

asp.net-mvc - 成功添加后将空模型传递回创建 View - 文本框仍然包含数据吗?

entity-framework-4 - 为什么将EntityState设置为“分离为空”,类型为List <T>的属性?

asp.net-mvc - 在 MVC 3 模型 ID 属性中将 ScaffoldColumn 属性设置为 False 的真正优势是什么

c# - ASP.Net MVC : How to read my custom claims value

.net - 是否可以检查对象是否已附加到 Entity Framework 中的数据上下文?

asp.net - 从 GridView 中的 2 列动态生成超链接

c# - 让 Entity Framework 在 Group By 上急切加载

c# - 启动后启用/禁用身份验证选项