views:

41

answers:

1

Say I have an User (mapped to a User table) and the Edit view (in MVC) displays a multiselectlist of Modules (mapped to a Modules table) that user can access, with the Modules pre-selected based on the User's EntitySet of Modules.

I have tried saving the User then deleting all User_Modules manually and adding them back based on what's selected on submit, but the User has a null EntitySet for User.User_Modules.

I cannot find the correct way to handle this scenario anywhere online. Can anyone help?

Edit: adding my EntitySet code

private EntitySet<UserModule> _UserModules;
    [Association(Storage = "_UserModules", ThisKey="UserId", OtherKey = "UserId")]
    public EntitySet<UserModule> UserModules
    {
        get { return this._UserModules; }
        set { this._UserModules.Assign(value); }
    }
A: 

Try checking if the set is loaded first

Linq to SQL

DataLoadOptions options = new DataLoadOptions();    
options.LoadWith<User>(c => c.User_Modules);
context.LoadOptions = options;

//Do query and then see if your EntitySet is still null.

Linq to entities way...

if(!User.User_ModulesReference.IsLoaded){
     User.User_ModulesReference.Load();
 }
 //Insert delete logic here...
Nix
My EntitySets never offer any functions/attributes such as IsLoaded or Load. I.e. adding a fullstop after User.User_Modules offers nothing in intellisense and adding .Load() for example won't build.Is this not expected behaviour?I built the EntitySet using the syntax from MSDN and double-checked against another tutorial. I can confirm the EntitySet does indeed contain a set of UserModules that I can query against.
Pete
Did you add them to both sides ? i.e. the relationship as well as the context?
Nix
I have no context highlighting for DataLoadOptions and no prompt to add a reference. I've edited my OP to show my EntitySet code on the class associated with the User table in case I'm doing or explaining it completely incorrectly.
Pete