0

While I am trying to delete my entity I am getting following error in my function;

A relationship from the 'ProjectWebsiteTag_ProjectUser' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'ProjectWebsiteTag_ProjectUser_Target' must also in the 'Deleted' state.

Here is my code to delete;

public bool Delete(int id)
        {
            try
            {
                using (ProjectDataContext context = new ProjectDataContext())
                {

                    ProjectWebsiteTag websiteTag = context.WebsiteTags.FirstOrDefault(p => p.WebsiteTagId == id);
                    context.WebsiteTags.Remove(websiteTag);
                    int saveChanges = context.SaveChanges();
                    return saveChanges > 0;
                }
            }
            catch (DbEntityValidationException e)
            {
                FormattedDbEntityValidationException newException = new FormattedDbEntityValidationException(e);
                throw newException;
            }
        }

Here is my data class;

public class ProjectWebsiteTag
    {
        public int WebsiteTagId { get; set; }
        public ProjectUser ProjectUser { get; set; }
        public ProjectWebsite ProjectWebsite { get; set; }           
    }

My Config Class;

 public ProjectWebsiteTagConfiguration()
        {
            ToTable("ProjectWebsiteTags");
            HasKey(p => p.WebsiteTagId);
            HasRequired(p => p.ProjectUser).WithRequiredDependent().WillCascadeOnDelete(false);
            HasRequired(p => p.ProjectWebsite).WithRequiredDependent().WillCascadeOnDelete(false);
        }

It looks like it is trying to delete User record, but I do not want that. I just want to delete "ProjectWebsiteTag" and that's it.

What I am missing here?

2
  • It doesn't seem to be a case of cascade deleting, it's trying to ADD a user with null values in non-nullable fields Commented Jul 6, 2015 at 3:29
  • @Rob Why it would even do that? I am not even intending to pull user entity from context. Commented Jul 6, 2015 at 3:34

2 Answers 2

1

It isn't trying to delete the ProjectUser. It's trying to insert it. ProjectWebsiteTagConfiguration() is saying that the ProjectWebsiteTags table has a ProjectUser foreign key in it. When you call

ProjectWebsiteTag websiteTag = context.WebsiteTags.FirstOrDefault(p => p.WebsiteTagId == id)

websiteTag has a ProjectUser with an empty string for its UserId property. So either the record in the ProjectWebsiteTags table has an empty string for the foreign key, or EF is newing a ProjectUser (with an empty UserId) when you get from the context. Either way, EF isn't aware of the existence of a ProjectUser with an empty string id, so when you call SaveChanges() it tries to add it. It can't because the UserId field is required and empty.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. I was initializing User object inside ProjectWebsiteTag constructor. I removed that initialization. Meanwhile, I am getting different error now. I updated the question. Thanks for the hint.
0

I had to fix my problem by adding one-to-many relationship configuration in my User class. It comes to a resolution of I need to add relationship hint on both sides of Configuration declarations.

public class ProjectUserConfiguration : EntityTypeConfiguration<ProjectUser>
    {
        public ProjectUserConfiguration()
        {
            ToTable("ProjectUsers");
            HasKey(p => p.UserId);
            HasMany(p=>p.ProjectWebsiteTags).WithRequired(q=>q.ProjectUser).WillCascadeOnDelete(false);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.