Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiplicity conflicts with referential constraint in Role

The Error message which i am getting is following ----- MVCRegistration.Models.Post_UserProfile: : Multiplicity conflicts with the referential constraint in Role 'Post_UserProfile_Target' in relationship 'Post_UserProfile'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. MVCRegistration.Models.PostComment_UserProfile: : Multiplicity conflicts with the referential constraint in Role 'PostComment_UserProfile_Target' in relationship 'PostComment_UserProfile'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. here, MVCRegistraion is solution file name.

Now, i have three classes like this---

 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; }
}

and the post class is something like this----

    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; }
  }

and PostComment is----

   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; }
}

I have configured one to many relationship betwween USerProfile class and Post class like this using fluent api---

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

and one to many relationship between USerProfile and Post comment class like this-----

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

Now, i am frustated figuring out what is going on here. It is so simple looking in code first to create foreign key with name PostedBy but entity framework is making things worse.Dont know what entity framework need now.

like image 615
duke Avatar asked Dec 01 '25 15:12

duke


2 Answers

As the statement said, in one to many end i.e at the dependent end, you have property non-nullable and in your fluent api, you are trying to make your foreign key nullable.

Just change your Foreign key in Post class from non-nullable to nullable like this--

 public int? PostedBy {get; set;}

and in your Comment class again u have to do it like this----

  pulic int? CommentedBy {get; set;}
like image 165
neo Avatar answered Dec 04 '25 08:12

neo


Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'

However:

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

since CommentedBy is not nullable, it means that the Post comment MUST HAVE a UserProfile. You have two options to fix this:

public int? CommentedBy { get; set; }

or

HasRequired(u => u.UserProfile).

It just has to match.

like image 25
DevilSuichiro Avatar answered Dec 04 '25 09:12

DevilSuichiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!