1

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?

Sam
  • 7,252
  • 16
  • 46
  • 65
Mad Halfling
  • 968
  • 3
  • 19
  • 36
  • 3
    You've asked 11 questions, have accepted no answers, and have never voted on anything. If you would like people here to spend time answering your questions, it would be a great idea to up vote those answers you find helpful and accept the answer you find most helpful. – Craig Stuntz Sep 24 '09 at 13:05
  • Sorry, I've been really busy on this project and sometimes don't have time to investigate/check answers immediately and after I didn't remember to go back and update the qs - sorry, I normally do this. I've gone through all my Qs and updated them, but unfortunately some of them can't be done, or the answers haven't given me the information I need. – Mad Halfling Sep 24 '09 at 13:32

1 Answers1

2

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
  • 125,891
  • 12
  • 252
  • 273
  • 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 Sep 25 '09 at 15:17
  • 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) Thanks MH – Mad Halfling Sep 25 '09 at 15:47
  • 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 Sep 25 '09 at 17:21
  • 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 Sep 28 '09 at 18:15