none
Entityframework Code First One to Many delete

    Question

  • He Experts,

    i am trying to build my first EntityFramework Code First Application. Its an WPF Application.
    Therefore i implemented a small Test Model. A Club can have many Competitors, and a competitor must have a Club, so from my
    point of view this looks like a standard 1:n relationship. My problem starts when i want delete competitors from a club.
    If i delete the whole club all competitors and the club rows are deleted from my database. But when i only want to delete
    a single competitor the framework throws a DBUpdateException:

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

    Here is my model:

    [Table("Competitor")]
    public class Competitor
    {
        public int Id { get; set; }
    
        public string Firstname  { get; set; }
        public string Lastname   { get; set; }
    
        public virtual Club Club { get; set; }
    
        public Competitor()
        {
    
        }
    }
    
    [Table("Clubs")]
    public class Club
    {
        public int Id      { get; set; }
    
        public string Name { get; set; }
    
        public virtual ObservableList<Competitor> Competitors { get; set; }
    
        public Club()
        {
            Competitors = new ObservableList<Competitor>();
        }
    }

    in the overridden OnModelCreating i added following line of code:

    modelBuilder.Entity<Club>().HasMany(c => c.Competitors).WithRequired(c => c.Club);

    at programstart I load all Entities into my application

        dbsCompetitors.Load();
        dbsClubs.Load();

    and then bind them to datagrids therefore i created a Getter that accesses the Local Part of DbSet

        public ObservableCollection<Club> Clubs
        {
            get { return dbsClubs.Local; }
        }
    my xaml code looks like this
    <DataGrid Grid.Column="0" Name="dgClubs" ItemsSource="{Binding Clubs}"/>
    <DataGrid Grid.Column="1" Name="dgCompetitors" ItemsSource="{Binding ElementName=dgClubs, Path=SelectedItem.Competitors}"/>
    when the application closes i just call
    _context.SaveChanges();

    Does anyone know what I am doing wrong?

    Kind Regards Manu



    Tuesday, May 28, 2013 8:06 AM

Answers

  • Hi Manu,

    I can see this is a master-detail form. The exception is generated because when you delete a row from dgCompetitors, it only delete the relationship between Club and Competitor. That is, competitor is removed from club's Competitors collection and competitor's Club property is set to null.

    The relationship is deleted however the Competitor entity is not removed. So you have to also remove the Competitor from the dbContext.

    foreach (Competitor c in _db.dbsCompetitors.Local.ToList())
    {
        if (c.Club == null)
        {
            _db.dbsCompetitors.Remove(c);
        }
    }
    
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 06, 2013 5:46 AM

All replies