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

  • Home
  • SEARCH
  • 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 8386005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:48:40+00:00 2026-06-09T17:48:40+00:00

My code worked fine until I added the WillCascadeOnDelete(true). Exception: InvalidOperationException – The database

  • 0

My code worked fine until I added the WillCascadeOnDelete(true).

Exception: InvalidOperationException – The database creation succeeded, but the creation of the database objects did not. See inner exception for more details.

Inner Exception: System.Data.SqlServerCe.SqlCeException – The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = User_AdministratorOf_Target ]

Minimal reproduction (In a new MVC3 Web app project):

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

public class User
{
    [Key]
    public string UserName { get; set; }
    public virtual ICollection<Document> Documents { get; set; }
    public virtual ICollection<Document> AdministratorOf { get; set; }
}

public class Document
{
    public int Id { get; set; }
    public User Owner { get; set; }
    public ICollection<User> Administrators{ get; set; }
}

public class EntityMappingContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Document> Documents { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .HasMany(x => x.AdministratorOf)
            .WithMany(x => x.Administrators)
            .Map(x =>
            {
                x.MapLeftKey("UserName");
                x.MapRightKey("Document");
                x.ToTable("DocumentAdministrators");
            });
        modelBuilder.Entity<Document>()
            .HasRequired(x => x.Owner)
            .WithMany(x => x.Documents)
            .WillCascadeOnDelete(true);
        base.OnModelCreating(modelBuilder);
    }

}

Also have to add the SQL connection string to web.config under connectionStrings of course:

<add name="EntityMappingContext" connectionString="Data Source=|DataDirectory|Error.sdf" providerName="System.Data.SqlServerCe.4.0"/>

How does enabling cascade on delete create a cyclic relationship when the one-to-many relationship already existed? Is it saying that there’s a cycle in the cascading delete? How, when the only cascade I’ve specified is User -> Document? How do I fix it? Thanks!

  • 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-09T17:48:42+00:00Added an answer on June 9, 2026 at 5:48 pm

    The cascade delete error is not necessarily due to cyclical relationship, but by having multiple references to the same table in a delete tree see this post.

    The trigger is Delete:

    • User -> Document -> DocumentAdministrator
    • User -> DocumentAdministrator.

    By deleting a user, it’s triggering the deletion of the many to many table twice, giving you the error. It may be better to just have the deletion of all documents belonging to the owner in your business logic.

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

Sidebar

Related Questions

The code below worked fine until I added the regex line. When I comment
The following code worked fine in IE7 until I started using IE9.js file. The
I have some code I am working on that worked just fine until I
My code worked fine in JQuery 1.3.2, but in 1.4.2 it seems to be
this piece of code worked fine on my machine, but it doesn't on my
The following code worked just fine thank you with one COM client, but with
so my stopwatch class worked fine until I made some changes in XML code
I have a file download code in my site which worked perfectly fine untill
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
My code worked fine when it was all in one file. Now, I'm splitting

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.