Operation failed: can't delete an entity from database.
I have this method classes:
public class Links
{
[Key]
public int LID { get; set; }
public string Link { get; set; }
public string Type { get; set; }
public string Filename { get; set; }
public virtual int RessourceId { get; set; }
}
public class Ressource
{
[Key]
public int RessourceId { get; set; }
public string TitreR { get; set; }
public string Desc { get; set; }
//public int Position { get; set; }
public int Rating { get; set; }
public string Tags { get; set; }
public virtual int SectionId { get; set; }
public virtual int UserId { get; set; }
public virtual ICollection<Links> Links { get; set; }
}
public class Section
{
[Key]
public int SectionId { get; set; }
public string Titre { get; set; }
public virtual ICollection<Tags> Tags { get; set; }
public virtual ICollection<Ressource> Ressources { get; set; }
//public Section() { this.Tag=new List<string>(); }
}
And when I want to delete a Ressource, I have this error:
The operation failed: The relationship could not be changed because one or
more of the foreign-key properties is non-nullable. When a change is made
to a relationship, the related foreign-key property is set to a null
value. If the foreign-key does not support null values, a new relationship
must be defined, the foreign-key property must be assigned another
non-null value, or the unrelated object must be deleted.
error line:
_db.Entry(R).State = EntityState.Deleted;
_db.SaveChanges(); // error line
PS: It was working before I added the Filename attribute to the Links
class... Any idea how to solve it? Thank you