0

I am completely lost why this error suddenly started coming.

I had Entity A which has one to Many relations with Entity B. It was working very nice but suddenly it stopped working. I can't even add/update an item in Enitity A nor i can't add/update in entity B. Only Get works fine. If i do Add or update for anything in there i get below error.

Have almost tried all the suggestion from stack overflow but nothing works.

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.

     public class ReportCategory : BaseEntity
     {
          public string Name { get; set; }

          public List<ReportType> ReportTypes { get; set; }
     }

     public class ReportType : BaseEntity
     {
            public string Name { get; set; }

           public int ReportCategoryId { get; set; }

         public string ReportUrl { get; set; }

         public ReportCategory ReportCategory { get; set; }

           public List<ReportAccess> reportAccesses { get; set; }

        }

entity add operation

        public void Add(T entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        entities.Add(entity);
        context.SaveChanges();
    }

update operation

           public void Update(T entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        if (entity.Id == 0)
        {
            throw new Exception("Entity unidentified");
        }
        if (entities.Local.Any(d => d.Id == entity.Id))
        {
            context.Entry(entities.Local.First(d => d.Id == entity.Id)).State = EntityState.Detached;
        }
        entities.Attach(entity);
        context.Entry(entity).State = EntityState.Modified;
        //entities.Attach(entity);
        context.SaveChanges();
    }

I would really appreciate if someone could help with this.

0 Answers0