views:

225

answers:

1
+1  Q: 

DeleteOnNull Error

I've got a set of DB objects sitting in an EntitySet on my main object definition. This handles additions and updates fine, but I found the removing items from the list didn't result in the database records being deleted, so I had to create a method in the data repository object to delete the records as the data object doesn't have access to the data-context in which it is being used. I was looking to see if I could bring this delete into the main object and I found the DeleteOnNull attribute to the association, but when I use it, I get an error "DeleteOnNull can only be true for singleton association members mapped to non-nullable foreign key columns". My code is:-

private EntitySet<UserSite> _userSites = new EntitySet<UserSite>();
[Association(Name = "User_UserSites", Storage = "_userSites", ThisKey = "UserID", OtherKey = "UserID", DeleteOnNull=true)]
public IList<UserSite> UserSites { get { return _userSites; } set { } }

my usersite object is

[Table(Name="UserSite")]
public class UserSite
{
    [Column]//(IsPrimaryKey = true)]
    public int UserID { get; set; }

    [Column]//(IsPrimaryKey = true)]
    public string Site { get; set; }

    [Column]
    public bool DefaultSite { get; set; }

    [Column(IsPrimaryKey = true, AutoSync = AutoSync.OnInsert)]
    public int UniqueID { get; set; }
}

Can I use DeleteOnNull to keep all my data update methods within my main user object, or do I have to handle the deletes at the repository level?

+2  A: 

DeleteOnNull is only for singleton associations. So you can put it on UserSite.User but not on User.UserSites. It's still not quite as automatic as you'd like it to be, though. There is an example here.

It's hard for LINQ to SQL to infer the behavior you want, because it can't guess if you want composition or aggregation, so it chooses the safe guess (aggregation).

Craig Stuntz
Thanks for that. I'm a bit confused though - the MS reference says (as I understand it) that it can only be places on a 1 -> 1 object relationship, but Dinesh implies (again, as I understand it - "If I remove one of the OderDetails from Order.OrderDetails collection") that it can be used on a 1 -> many relationship, or is this slightly innocuously phrased? The error I am getting infers that it has to be a 1 -> 1, is this the case
Mad Halfling
Just to clarify - "So you can put it on UserSite.User" - in this case you would be deleting the User, correct?So I'm a bit stuffed form deleting a User.UserSites record, except via the datacontext?(sorry if my comments are a bit disjointed, trying to work around a whole bunch of issues with the MVC ATM)ThanksMH
Mad Halfling
Re: 1..1 vs. 1..*. Yes, what the docs and Dinesh says do differ. Try it! Regarding deleting User, no, you won't delete User. User is like Order in his example, and UserSite is like OrderDetail. Note carefully which one he deletes!
Craig Stuntz
I tried it and trying to set DeleteOnNull to an EntitySet gives an immediate compilation error. It's slightly frustrating that Dinesh doesn't post his code to create the link...
Mad Halfling