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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:13:42+00:00 2026-05-29T04:13:42+00:00

thanks for attention. During developing an accounting program, I faced with a flowing problem

  • 0

thanks for attention.
During developing an accounting program, I faced with a flowing problem and need some help engineers.

I have to entities : Product and unit

 [Table("Products")] 
public class Product
{

    #region Properties

    private int _Id;
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
    public int Id
    {
        get { return _Id; }
        set { _Id = value; }
    }

    private string _Name;
    [Required]
    [MaxLength(50)]
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    private string _Code;
    [Required]
    [MaxLength(20)]
    public string Code
    {
        get { return _Code; }
        set { _Code = value; }
    }      

    private int _MainUnitId;
    [Required]
    public int MainUnitId
    {
        get { return _MainUnitId; }
        set { _MainUnitId = value; }
    }

    private int _SubsidiaryUnitId;
    [Required]
    public int SubsidiaryUnitId
    {
        get { return _SubsidiaryUnitId; }
        set { _SubsidiaryUnitId = value; }
    }

    private int _SnachUnitId;
    [Required]
    public int SnachUnitId
    {
        get { return _SnachUnitId; }
        set { _SnachUnitId = value; }
    }

    private decimal _SubidiaryCount;
    [Required]
    public decimal SubidiaryCount
    {
        get { return _SubidiaryCount; }
        set { _SubidiaryCount = value; }
    }

    private decimal _SnachCount;
    [Required]
    public decimal SnachCount
    {
        get { return _SnachCount; }
        set { _SnachCount = value; }
    }               


    #endregion Proerties

    #region Navigators

    private Unit _MainUnit;
    [ForeignKey("MainUnitId")]
    public virtual Unit MainUnit
    {
        get { return _MainUnit; }
        set { _MainUnit = value; }
    }

    private Unit _SubsidiaryUnit;
    [ForeignKey("SubsidiaryUnitId")]
    public virtual Unit SubsidiaryUnit
    {
        get { return _SubsidiaryUnit; }
        set { _SubsidiaryUnit = value; }
    }



    private Unit _SnachUnit;
    [ForeignKey("SnachUnitId")]
    public virtual Unit SnachUnit
    {
        get { return _SnachUnit; }
        set { _SnachUnit = value; }
    }

    #endregion Navigators

}

and this is my Unit Entity :

[Table("Units")]
public class Unit
{

    private int _Id;

    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
    public int Id
    {
        get { return _Id; }
        set { _Id = value; }
    }

    private string _Title;

    [Required]
    [MaxLength(50)]
    public string Title
    {
        get { return _Title; }
        set { _Title = value; }
    }

    private ICollection<Product> _MainUnitedProducts;
    [InverseProperty("MainUnit")]
    public virtual ICollection<Product> MainUnitedProducts
    {
        get { return _MainUnitedProducts; }
        set { _MainUnitedProducts = value; }
    }

    private ICollection<Product> _SubsidiaryUnitedProducts;
    [InverseProperty("SubsidiaryUnit")]
    public virtual ICollection<Product> SubsidiaryUnitedProducts
    {
        get { return _SubsidiaryUnitedProducts; }
        set { _SubsidiaryUnitedProducts = value; }
    }

    private ICollection<Product> _SnachUnitedProducts;
    [InverseProperty("SnachUnit")]
    public virtual ICollection<Product> SnachUnitedProducts
    {
        get { return _SnachUnitedProducts; }
        set { _SnachUnitedProducts = value; }
    }

}

As you see i have have tree one-to-many relation between these entitis :

Product.MainUnitId        *-----1     Unit.Id
Product.SubsidiaryUnitId  *-----1     Unit.Id
Product.SnachUnitId       *-----1     Unit.Id

but when i use the Context.Database.Create() an unexpected error occurred

Introducing FOREIGN KEY constraint ‘FK_Products_Units_SubsidiaryUnitId’ on table ‘Products’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Is there anyone out there to help me?!
Did I use InverProperty mapping and ForeignKey mapping right?

thanks guys

Foroughi

  • 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-29T04:13:43+00:00Added an answer on May 29, 2026 at 4:13 am

    The exception comes from SQL Server. You will have to turn off cascading delete for the three relationships (it is on by mapping conventions because your relationships are required and not optional). Unfortunately you can’t do this with data annotations but only with Fluent API:

    modelBuilder.Entity<Product>()
        .HasRequired(p => p.MainUnit)
        .WithMany(u => u.MainUnitedProducts)
        .HasForeignKey(p => p.MainUnitId)
        .WillCascadeOnDelete(false);
    

    And the same for the other two relationships. You can remove then the [ForeignKey] and [InverseProperty] annotations because this mapping in Fluent API already defines the inverse and foreign key properties.

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

Sidebar

Related Questions

Hello all and thanks for the attention! I have a problem that must both
Thanks for your attention and time. I need your opinion on an basic architectural
I have several newbie questions about EC2, thanks for your attention, 1) why EC2
I have strange problem with XUL layouts. My current code: <xul:vbox> <xul:hbox> .. some
thanks for your attention and precious time. Please mention some free javascrpt obfuscator software
Thanks for reading this. I am dynamically generating some data which includes a select
thanks in advance for your help. I am wondering if there is a (design)
Thanks to a library upgrade (easymock 2.2 -> 2.4), we're having tests that have
Thanks for reading this. I have markup similar to what is below. Using the
Thanks for your attention and time. I'm modifying an existing JavaScript but can't understand

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.