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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:40:00+00:00 2026-05-28T16:40:00+00:00

Here is the approx. code I am working with. public class Note { public

  • 0

Here is the approx. code I am working with.

public class Note {
  public virtual Customer Customer { get; set; }
  public virtual User User { get; set; }
  public ICollection<NoteComment> Comments { get; set; }
}

public class NoteComment {
  public virtual User User { get; set; }
}

public class User {
  public ICollection<Note> Notes { get; set; }
}

public class Customer {}

// --------------------------------------

public class OurDataContext {
  private void ConfigureNotes(DbModelBuilder modelBuilder) {
    modelBuilder.Entity<Note>()
      .HasRequired<User>(n => n.User)
      .WithMany(u => u.Notes)
      .Map(a => a.MapKey("UserId"));

    modelBuilder.Entity<Note>()
      .HasRequired(n => n.Customer)
      .WithMany(c => c.Notes)
      .Map(a => a.MapKey("idCustomer"));

    modelBuilder.Entity<Note>()
      .HasMany(n => n.Comments)
      .WithRequired()
      .HasForeignKey(c => c.NoteID);    
/*
    modelBuilder.Entity<NoteComment>()
      .HasRequired<User>(c => c.User)
      .WithMany()
      .Map(a => a.MapKey("UserId"));
*/
  }
}

}

Note that in the ConfigureNotes() method, the last configuration is commented out. If I leave this commented out, EF will create my tables just fine, but if I uncomment this block, I get the following error:

Unhandled Exception: System.InvalidOperationException: The database creation succeeded, but the creation of the database objects did not. See inner exception for more details. ---> System.Data.SqlServerCe.SqlCeException: The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = Note_Comments ]
   at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()
   at System.Data.SqlServerCe.SqlCeProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 timeOut, StoreItemCollection storeItemCollection)
   --- End of inner exception stack trace ---
   ...

What I don’t understand is why the navigation property from NoteComment => User is generating a circular reference between Note => NoteComment.


EDIT

For some reason, specifying the FK in the NoteComment class as a nullable property fixed the problem.

public class NoteComment {
  public Guid? UserId { get; set; }
  [ForeignKey("UserId")]
  public virtual User User { get; set; }
}

Then I removed the commented out mapping code in the data context class.

This is not ideal, but I can manage this constraint manually.

  • 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-28T16:40:01+00:00Added an answer on May 28, 2026 at 4:40 pm

    SQL Server is very conservative about possible circular references or multiple delete paths compared to other databases.

    Yours is originating from multiple delete paths to NoteComment:

    Delete User -> Note -> NoteComment
    Delete User -> NoteComment
    

    One solution is to remove the Cascade On Delete for User -> NoteComment and do the cleanup manually.

    You can also write a database trigger to do the cleanup. Here’s an example trigger:

    CREATE TRIGGER [dbo].[Users_Delete_Cleanup] 
       ON  [dbo].[Users]
       INSTEAD OF DELETE
    AS 
    BEGIN
        IF @@ROWCOUNT = 0
            RETURN
    
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Delete NoteComment <--> User associations
        DELETE nc FROM [dbo].[NoteComment] nc
        JOIN DELETED dUser ON dUser.[Id] = nc.[User_Id]
    
        -- Finally, delete user
        DELETE u
        FROM DELETED dUser
        JOIN [dbo].[Users] u ON u.[Id] = dUser.[Id]
    END
    

    Edits – additional information:

    If you don’t have it already, I highly suggest the EF Power Tools extension. This gives you the ability to right click on any class implementing DbContext and get the Entity Framework context menu –
    Right click your DbContext class in Solution Explorer -> Entity Framework -> View DDL SQL

    That will give you the Sql statement used to generate your entire data model – very useful indeed to see what exactly EF thinks it’s building. You can try and run it manually in SqlServer and get a bit closer to the errors that it’s running into. When EF is building up the DDL Sql, short of compile errors it will usually get you something (or an entirely cryptic null reference error – then check your Output window), but that something might not run in SqlServer.

    Also, you can manually remove the cascade on delete for one to many relationships with the fluent configuration, no need to specify keys unless you want that property:

    modelBuilder.Entity<NoteComment>()
        .HasRequired<User>(c => c.User)
        .WithMany()
        .WillCascadeOnDelete(false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code, which takes two version identifiers in the form 1, 5,
I am working on a C# application and facing a strange issue here. I
I am working on maintaining someone else's code that is using multithreading, via two
I have the code below running in my application and it takes approx 2
Okay so here's the thing: I have a linq query which loads approx. 1000
As discussed in similar questions here and here I want to protect my code
Background: Inspired from Apple's sample code ScrollViewSuite, I've created a view controller class that
I'm working on some Google Web Toolkit Code that places an AbsolutePanel on top
I’m working with the .NET 4.0 MemoryCache class in an application and trying to
Here's the problem: we have a family (approx. 8) of websites, each hosted on

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.