How to solve “FOREIGN KEY constraint may cause cycles or multiple cascade paths”

In code first once i got an error

Introducing FOREIGN KEY constraint ‘FK_dbo.Regions_dbo.Countries_CountryId’ on table ‘Regions’ 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. See previous errors.

web_services[1]
After some searches I found many options to solve this issue. One of simple and global solution is add modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>() in OnModelCreating method of your DataContext file as follow:

public class YourDataContext : DbContext
{
    public DbSet<Country> Countries{ get; set; }
    ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

    }
}
Advertisements

Set Access-Control-Allow-Origin in Web API

If you access your web api methods from another domain using JQuery Ajax or GetJSON method then there you may see an error like
XMLHttpRequest cannot load (url). No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘(url)’ is therefore not allowed access.

You can just install CORS with package manager

Install-Package Microsoft.AspNet.WebApi.Cors

Follow this link http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Or create a own simple attribute like

  • Add a new class AllowCrossSiteJsonAttribute
    public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
            {
                if (actionExecutedContext.Response != null)
                    actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
    
                base.OnActionExecuted(actionExecutedContext);
            }
        }
    
  • For whole API Controller
    [AllowCrossSiteJson]
    public class UserAPIController : ApiController
    {
            // GET api/userapi
            public HttpResponseMessage Get()
            {
                  return uservar;
            }
    }
    
  • For a specific API Call
    public class UserAPIController : ApiController
    {
             // GET api/userapi
             [AllowCrossSiteJson]
             public HttpResponseMessage Get()
             {
                   return uservar;
             }
    }
    

that’s it.

String Formating in c# 6

Formatting the string values are very common during development. We generally use String.Format() method, that replaces each format item in a specified string with the text equivalent values from the objects. While this is a common, sometimes this is confusing and produce error. Because, we need to specific the place holder for each item and […]

Rate this:

Learn ASP.NET MVC 5 in 2 days

Questpond has launched a full step by step course on ASP.NET MVC. This course from Shivprasad Koirala will help you to learn MVC (Model view controller) step by step in 2 days ( i.e in 16 hours) right from basics to a professional level. This is a Sponsored post and in this post, the first video will show you how to display a simple Hello world in ASP.NET MVC. Also, check out the complete syllabus for details. Shivprasad Koirala has published a video course on Questpond, which will step-by-step guide you to learn MVC 5 in just 2 days
(yes, you heard the word correct… just 2 days i.e. 16 hours only). Below is the complete syllabus of the course “Learn MVC 5 in 2 days”:Lab 01 : – Simple Hello world (20 minutes) Lab 02 : – Explain MVC Routing (10 minutes) Lab 03 : – Explain ViewData, ViewBag, TempData & Session Variables (20 minutes) Lab 04 : – Explain Model and Strongly typed views (20 minutes) Lab 05 : – Explain Model Binders (10 minutes) Lab 06 : – Why MVC and MVC vs Webforms? (30 minutes) Lab 07 : – Explain TempData, Peek and…(Read more)