Question

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?

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top