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 6554853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:44:59+00:00 2026-05-25T12:44:59+00:00

I’m new to EF code first principal and currently with no clue what to

  • 0

I’m new to EF code first principal and currently with no clue what to do.. I have 2 POCO classes..

public class Problem
{
    public int ProblemID { get; set; }
    public int UserID { get; set; }
    public int CategoryID { get; set; }
    public int RatingID { get; set; }

    public string Title { get; set; }
    public string Description { get; set; }        
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public int State { get; set; }
    public DateTime CreatedOn { get; set; }

    public virtual Rating Rating { get; set; }
    public virtual Category Category { get; set; }
    public virtual User User { get; set; }

    public virtual ICollection<Picture> Pictures { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

and

public class User
{
    public int UserID { get; set; }
    public int RoleID { get; set; }

    public string Name { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string SocialHandle { get; set; }       
    public DateTime RegisteredOn { get; set; }

    public virtual Role Role { get; set; }

    public virtual ICollection<Point> Points { get; set; } 
    public virtual ICollection<Problem> Problems { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }               
}

My database creation is ok, but when I try to init some data in it using custom initializer, I’m getting following error :

Introducing FOREIGN KEY constraint ‘Problem_User’ on table
‘Problems’ may cause cycles or multiple cascade paths. Specify ON
DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY
constraints.
Could not create constraint. See previous errors.

This is my initializer :

protected override void Seed(CleanStreets context)
    {
        var adminRole = new Role { Name = "Administrator", RoleID = 0 };            
        var moderatorRole = new Role { Name = "Moderator", RoleID = 1 };
        var userRole = new Role { Name = "User", RoleID = 2 };

        context.Roles.Add(adminRole);
        context.Roles.Add(userRole);
        context.Roles.Add(moderatorRole);

        var admin = new User { Name = "admin", Surname = "admin", Role = adminRole, Email = "fake@fake.com", Password = "", RegisteredOn = DateTime.Now, SocialHandle = null };
        var user = new User { Name = "user", Surname = "user", Role = userRole, Email = "fake@fake2.com", Password = "", RegisteredOn = DateTime.Now, SocialHandle = null };

        context.Users.Add(admin);
        context.Users.Add(user);

        var categoryOne = new Category { Title = "Komunalni problemi", CategoryID = 0 };
        var categoryTwo = new Category { Title = "Ostali problemi", CategoryID = 1 };

        context.Categories.Add(categoryOne);
        context.Categories.Add(categoryTwo);

        var problem = new Problem { Category = categoryOne, Title = "Prvi testni problem", Description = "Ovo je testni opis", Latitude = 45.5, Longitude = 15.5, State = 0, CreatedOn = DateTime.Now, User = user };

        context.Problems.Add(problem);

        base.Seed(context);
    }

What I’m doing wrong? Thank you in advance!

  • 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-05-25T12:45:00+00:00Added an answer on May 25, 2026 at 12:45 pm

    That will be because of Comments. EF by default uses cascade deletes on references. In your case the cascade delete will be created from User -> Problem, User -> Comment but also from Problem -> Comment. If you deleted User cascading to the same comment record can come from both Problem and User. That is not allowed in SQL Server. Each record can be accessible by only single cascade delete path.

    To avoid this you must use fluent mapping to turn of cascade delete on one relation (you must make choice which one). The example for User -> Comment

    public class Context : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<Problem> Problems { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                        .HasMany(u => u.Comments)
                        .HasRequired(c => c.User)
                        .HasForeignKey(c => c.UserId)
                        .WillCascadeOnDelete(false);
    
            base.OnModelCreating(modelBuilder);
        }
    } 
    

    There can be other entities and relations which can cause this problem in your model but Comment looks obvious from your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.