none
I am Getting below mentioned error while using Code First In MVC. kindly Tell me solution RRS feed

  • Question

  • Introducing FOREIGN KEY constraint 'FK_dbo.Purchases_dbo.Suppliers_SupplierId' on table 'Purchases' 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 or index. See previous errors.
    Thursday, January 2, 2020 10:22 AM

All replies

  • Well since you are using code first, then you need to show the code for the model and code where you have the problem in accessing the model.

     
    Thursday, January 2, 2020 1:22 PM
  • Hi Mehmood.moodi,
    Based on your description, there are many reasons for this error. So could you provide the relevant code that caused the error?
    Besides, you can refer to the usage of ForeignKey Attribute in using code first.
    [Data Annotations - ForeignKey Attribute in EF 6 & EF Core]
    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 3, 2020 5:14 AM
  • You need to update the OnModelCreating method in your context and using the fluent API add the OnDelete behavior.

    Here is an example:

        public class Author
        {
            public int AuthorId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public ICollection<Book> Books { get; set; }
        }
    
        public class Book
        {
            public int BookId { get; set; }
            public string Title { get; set; }
        }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Book>()
                .HasOne(b => b.Author)
                .WithMany(a => a.Books)
                .IsRequired()
                .OnDelete(DeleteBehavior.Delete);
        }


    william xifaras

    Monday, January 6, 2020 9:46 PM