12 Ekim 2016 Çarşamba

Entity Framework: Solution of foreign key constraint may cause cycles or multiple cascade paths?





While I was working on a Project that is ASP.NET MVC using Entity Framework as ORM, I have encountered an error was writen on nuget packet  manager console.It says 'Introducing FOREIGN KEY constraint 'FK_dbo.Skill_dbo.Image_ImageId' on table 'Skill' 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.' which spotted after I typed 'update-database' commend to update my tables on sql server.

My code contains two class here.As shown below,these tables are Skill and Image.

public class Skill


   {
        [
Key]

         public int Id { get; set; }

         [
MaxLength(50)]

         public string Name { get; set; }

        [
MaxLength(50)]

        public string Title { get; set; }

       [
MaxLength]

       public string Description { get; set; }

       [
MaxLength]

       public string Lore { get; set; }

  
    [ForeignKey("ImageId")]
      public virtual Image Image { get; set; }

      public int ImageId { get; set; }


      [
MaxLength(20)]

      public string ManaCost { get; set; }

      [MaxLength(20)]

       public string CoolDown { get; set; }

}



public class Image
{
      [MaxLength(2083)]

      public string OriginalSource { get; set; }

      [MaxLength(2083)]

       public string ServiceSource { get; set; }

       [MaxLength(2083)]

       public string AppSource { get; set; }

       [Key]

       public int Id
      {
        
get;

         set;
       }



       [
MaxLength(50)]

        public string Name
      {
        
get;

         set;
       }



       
public DateTime? CreateDate
       {
        
get;

         set;
         }



        
public DateTime? UpdateDate
       {
        
get;

         set;
        }
}  


! SOLUTION : Skill table has a relationship with Image table but Image table does not.Assume that a skill has image data whose id is 77.After that I want to remove the image with id=77,what is gonna happen? Lets discuss what we have until now.We have Skill and Image tables.Inside Skill table there is a skill data that has an image whose id is 77.Inside the Image table we have an image as we said before number 77. I  want to remove id's 77 from the Image table.But also we have not NULLABLE image column inside Skill table.The skill row that is connected with the image data  will not reach to the image date after removing process.Thus,SQL wont give us permission to do this.Solution is simple. We need to allow image column to be nullable inside Skill table.That give us chance to delete an image data and set skill data's image column NULL.Just do it inside Skill class as you see below.

public int? ImageId{get;set;}   or public Nullable<int> ImageId{get;set;}

If your problem looks more complicated than mine or my solution is not fit for your code also I prefer you to check this link out.




Hiç yorum yok:

Yorum Gönder