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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:35:15+00:00 2026-06-13T17:35:15+00:00

I have following entity class called Code . It stores categories of different kinds

  • 0

I have following entity class called Code. It stores categories of different kinds – the data for which I would have otherwise needed to create many small tables e.g. User Categories, Expense Categories, Address types, User Types, file formats etc.

public class Code
{
    public int Id { get; set; }
    public string CodeType { get; set; }
    public string CodeDescription { get; set; }


    public virtual ICollection<Expense> Expenses { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
                   :
                   : // many more

}

The class Expense looks like this:

public class Expense
{
    public int Id { get; set; }

    public int CategoryId { get; set; }
    public virtual Code Category { get; set; }

    public int SourceId { get; set; }

    public double Amount { get; set; }
    public DateTime ExpenseDate { get; set; }
}

With the above class definitions, I have established 1:many relation between Code and Expense using the CategoryId mapping.

My problem is, I want to map the SourceId field in Expense to the Code object. Which means, Expense object would contain

public Code Source { get; set; }

If I use this, at runtime I get an error about cyclic dependencies.

Can someone please help?

  • 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-13T17:35:16+00:00Added an answer on June 13, 2026 at 5:35 pm

    You will need to disable cascading delete on at least one of the two relationships (or both). EF enables cascading delete by convention for both relationships because both are required since the foreign key properties are not nullable. But SQL Server doesn’t accept multiple cascading delete paths onto the same table that are introduced by the two relationships. That’s the reason for your exception.

    You must override the convention with Fluent API:

    public class Code
    {
        public int Id { get; set; }
        //...
        public virtual ICollection<Expense> Expenses { get; set; }
        //...
    }
    
    public class Expense
    {
        public int Id { get; set; }
    
        public int CategoryId { get; set; }
        public virtual Code Category { get; set; }
    
        public int SourceId { get; set; }
        public virtual Code Source { get; set; }
        //...
    }
    

    Mapping with Fluent API;

    modelBuilder.Entity<Expense>()
        .HasRequired(e => e.Category)
        .WithMany(c => c.Expenses)
        .HasForeignKey(e => e.CategoryId)
        .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Expense>()
        .HasRequired(e => e.Source)
        .WithMany()
        .HasForeignKey(e => e.SourceId)
        .WillCascadeOnDelete(false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to entity frame work code first. I have simple class called Cat
I have the following entity class : @Entity @Table(name = THE_TREE, catalog = ,
Say, I have following entities: @Entity public class A { @Id @GeneratedValue private Long
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
I have the following entities: @Entity public class Owner{ @Id @Column(name = OWNER_ID) @OneToMany()
I have a JPA entity bean similar to the following: @Entity class License {
I have two entity classes annotated in the following way @Entity class A {
Suppose I have the following 2 entities: @Entity public class Person implements Serializable {
I have the following classes: public class Entity<T> where T : Entity<T> { public
I have the following mapping: @Entity @Table(name = Prequalifications) public class Prequalification implements Serializable

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.