Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8800275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:32:39+00:00 2026-06-14T00:32:39+00:00

I’m getting the following error when running my application. I’m forcing the application to

  • 0

I’m getting the following error when running my application. I’m forcing the application to do a DropCreateDatabaseAlways for my context so I’m not sure why it would say that I’m trying to insert multiple entities with the same key.

There might be a problem with how User and Message are linked. I had to add:

                modelBuilder.Entity<Message>()
                    .HasRequired(m => m.UserSentFrom)
                    .WithRequiredDependent()
                    .WillCascadeOnDelete(false);

Just because I was getting a warning about circular reference and had to set the willcascadeondelete to false.

If there is something wrong with my design, let me know, but this is what I’m trying to do:
User has many Messages,
Message has one FromUser
Message has many ToUsers

Any help will be appreciated as this is driving me crazy.

{“Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.”}

    public class User
        {
            public int Id { get; set; }
            [Required(ErrorMessage="Username is required")]
            public string Username { get; set; }
            [Required(ErrorMessage = "Password is required")]
            [DataType(DataType.Password)]
            public string Password { get; set; }
            [Required(ErrorMessage="Email Address is required")]
            [DataType(DataType.EmailAddress)]
            public string UserEmailAddress { get; set; }
            [DisplayFormat(DataFormatString="{0:d}",ApplyFormatInEditMode=true)]
            public DateTime DateCreated { get; set; }
            [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
            public DateTime LastUpdated { get; set; }
            public virtual ICollection<Message> Messages { get; set; }
            public User()
            {
                Messages = new List<Message>();
            }
        }  

    public class Message
        {
            public int Id { get; set; }
            public DateTime DateSent { get; set; }
            public virtual ICollection<User> UserSentTo { get; set; }
            public string Title { get; set; }
            public string Body { get; set; }
            public int UserSentFromId { get; set; }
            public virtual User UserSentFrom { get; set; }
        }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Message>()
                     .HasMany(m => m.UserSentTo).WithMany(u => u.Messages)
                     .Map(t => t.MapLeftKey("MessageId")
                         .MapRightKey("UserId")
                         .ToTable("MessagesUserSentTo"));
                modelBuilder.Entity<Message>()
                    .HasRequired(m => m.UserSentFrom)
                    .WithRequiredDependent()
                    .WillCascadeOnDelete(false);
            }

    protected override void Seed(WebsiteGameContext context)
            {
                var users = new List<User>
                {
                    new User { Username = "Admin", UserEmailAddress="admin@temp.com", DateCreated = DateTime.Now, LastUpdated = DateTime.Now},
                    new User { Username = "Member", UserEmailAddress="member@temp.com",DateCreated = DateTime.Now, LastUpdated = DateTime.Now}
                };
                users.ForEach(u => context.Users.Add(u));
                context.SaveChanges();

                var messages = new List<Message>
                {
                    new Message { DateSent=DateTime.Now, Title="Sent From Admin", Body="There should be 2 users that this was sent to", UserSentFromId=users[0].Id,UserSentTo= new List<User>()},
                    new Message { DateSent=DateTime.Now, Title="Sent From Member", Body="There should be 1 user that this was sent to", UserSentFromId=users[1].Id,UserSentTo= new List<User>()}
                };
                messages.ForEach(m => context.Messages.Add(m));
                context.SaveChanges();

                messages[0].UserSentTo.Add(users[0]);
                messages[0].UserSentTo.Add(users[1]);
                messages[1].UserSentTo.Add(users[0]);
                context.SaveChanges();
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:32:40+00:00Added an answer on June 14, 2026 at 12:32 am

    To make it work change 2 things.
    First delete the messages property in Users and make a MessagesSend and MessagesReceived property. Then make the mapping like this:

            modelBuilder.Entity<Message>().HasMany(t => t.UserSentTo)
                .WithMany(t => t.MessagesReceived)
                .Map(m =>
                {
                    m.ToTable("MessagesUserSentTo");
                    m.MapLeftKey("MessageId");
                    m.MapRightKey("UserId");
                });
    
            modelBuilder.Entity<Message>().HasRequired(t => t.UserSentFrom)
                .WithMany(t => t.MessagesSend)
                .HasForeignKey(d => d.UserSentFromId)
                .WillCascadeOnDelete(false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.