none
Lazy Loading and referential integrity constraint violation. RRS feed

  • Question

  • In my application I have the following classes:

    public partial class Bar
        {
            public Bar()
            {
                this.FooRows = new HashSet<FooRow>();
            }
        
            public int BarId { get; set; }
            public string Name { get; set; }
        
            public ICollection<FooRow> FooRows { get; set; }
        }
    
    public partial class FooRow
        {
            public int FooRowId { get; set; }
            public int BarId { get; set; }
            public string Street { get; set; }
            public string Color { get; set; }
        
            public virtual Bar Bar { get; set; }
        }

    Using Lazy Loading load correctly both the main entity(FooRow) that the details(Bar).So far so good....

    private void button1_Click(object sender, EventArgs e)
            {
                using (var ctx = new MyContext())
                {
                    //ctx.Configuration.LazyLoadingEnabled = false;
                    f = (from c in ctx.FooRows
                        where c.FooRowId == 1
                        select c).Single();
                    
                    BindingSource.DataSource = f;
                }
            }

    But when I try tos ave changes

    private void button2_Click(object sender, EventArgs e)
            {
                using (var ctx = new MyContext())
                {
                    ctx.Entry(f).State = EntityState.Modified;
                    ctx.SaveChanges();
                }
            }

    Violation of the referential integrity constraint. The property values ​​that define the referential constraints are not consistent between principal and dependent objects of the report.

    What is wrong?



    • Edited by piedatt80 Friday, April 26, 2013 2:39 PM Error in text
    Friday, April 26, 2013 2:37 PM

Answers

All replies