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>();

    }
}

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.