Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:22:13+00:00 2026-05-24T05:22:13+00:00

I have the following classes in Entity Framework 4.1 (the classes have been pruned

  • 0

I have the following classes in Entity Framework 4.1 (the classes have been pruned to keep the code readable)

public class MetaInformation
{
    public int Id { get; set; }
    public virtual MetaInformationObject RelatedTo { get; set; }
}

public abstract class MetaInformationObject
{
    public int Id { get; set; }
    public virtual List<MetaInformation> MetaInformations { get; set; }
}

[Table("Hardwares")]
public class Hardware : MetaInformationObject
{
    [MaxLength(100)]
    public string Name { get; set; }
}

public class Inventory
{
    public int Id { get; set; }
    public virtual List<Machine> Machines { get; set; }
}

public class Machine : Hardware
{
    public Inventory Inventory { get; set; }
}

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Isis.Business.Base.MetaInformationObject>()
        .HasMany<Isis.Business.Base.MetaInformation>(mi => mi.MetaInformations).WithRequired(mi => mi.RelatedTo).WillCascadeOnDelete(true);
    modelBuilder.Entity<Isis.Business.Inventory.Inventory>()
            .HasMany<Isis.Business.Inventory.Machine>(i => i.Machines).WithRequired(m => m.Inventory).WillCascadeOnDelete(true);

    base.OnModelCreating(modelBuilder);
}

At one point, i’d like to update a machine information, so if it already exists in the database, i load it, attach it and then clear the previous metainformations to replace them with the new ones.

public void UpdateMachine(Inventory i, Machine m)
{
    DataContext.Dao.Db.Inventories.Attach(i);
    if (!i.Machines.Exists(InnerHardware => InnerHardware.SerialNumber == m.SerialNumber)) {
        i.Machines.Add(m);
    } else {
        var workingMachine = i.Machines.First(Machine => Machine.SerialNumber == m.SerialNumber);
        Dao.Db.Machines.Attach(workingMachine);
        if (workingMachine.MetaInformations != null && workingMachine.MetaInformations.Count > 0) {
            workingMachine.MetaInformations.Clear();
            //workingMachine.MetaInformations.ForEach(mi => { Dao.Db.MetaInformations.Attach(mi); Dao.Db.MetaInformations.Remove(mi); }); // tried this to, with variations
        }
        workingMachine.MetaInformations = m.MetaInformations;
    }
    DataContext.Dao.Db.SaveChanges();
}

And then, the following DbUpdateException is thrown:

A relationship from the ‘MetaInformationObject_MetaInformations’
AssociationSet is in the ‘Deleted’ state. Given multiplicity
constraints, a corresponding
‘MetaInformationObject_MetaInformations_Target’ must also in the
‘Deleted’ state.

I tried following some questions here on SO to solve the problem, especially trying to read this one and the link provided in the answer (that’s the reason why there’s a direct reference from MetaInformation to MetaInformationObject) but i can’t figure out what’s wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:22:13+00:00Added an answer on May 24, 2026 at 5:22 am

    Cascade delete will not help you here. Cascade delete works only if you delete parent entity (MetaInformationObject). The problem here is that calling Clear on the collection of related objects doesn’t delete them. It only deletes the relation (= it will set foreign key in MetaInformation to null) but it is not allowed by your mapping constraints (you defined relation as required). The ways to avoid this are either:

    • Iterate through all related MetaInformations and delete each of them
    • Refactor you model to define identifying relation. After that Clear will not only break the relation but mark related entity as deleted as well.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Entity Framework POCO classes: public class Customer { public int
I have the following classes in my Model: public abstract class Entity : IEntity
I have the following annotated Hibernate entity classes: @Entity public class Cat { @Column(name
I have two entity classes annotated in the following way @Entity class A {
I have following classes. class A { public: void fun(); } class B: public
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following classes: public class Person { public String FirstName { set;
I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>
I have the following classes: public class Person { [Key] public Guid Id {
Here is my problem in C# : I have the following classes: public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.