FransBouma / LinqToSQL2

Official Linq to SQL fork. A complete ORM which is backwards compatible with Linq to SQL but with new features.
Other
57 stars 22 forks source link

Prevent phantom inserts of empty entities #22

Open FransBouma opened 9 years ago

FransBouma commented 9 years ago

This test fails

[Test]
public void PhantomInsertPreventionTest()
{
    var newCustomer = new Customer();
    var ba = new Address();
    var va = new Address();
    newCustomer.VisitingAddress = va;
    newCustomer.BillingAddress = ba;

    // as everything is empty, and Address isn't saved because it's not dirty, customer shouldn't be saved as well.
    using(var ctx = GetContext())
    {
        ctx.Customers.InsertOnSubmit(newCustomer);
        ctx.SubmitChanges();
        Assert.AreEqual(0, ba.AddressId);
        Assert.AreEqual(0, va.AddressId);
    }
}

because Linq to Sql happily inserts Address, while it's not dirty and has nonnullable fields. No save should occur at all, as there are only empty entities to persist. Preventing these 'phantom' inserts will avoid the user running into either empty rows (because there were nullable fields so the persist succeeded), or have to check up front whether a save is necessary.