This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Let's take two examples: 1) Group groupFound = application.Groups.Find(Group.ByGroupName("New Group")); Here the Groups property is an EntityCollection<Group>, so I can search and modify Group objects directly and SaveChanges will post those changes back to the database. 2) User userFound = groupFound.Users.Find(User.ByUserName("Test User")); Here the Users property is a ThroughAssociation<UserGroup, User>, so the Find method is not available. I've tried a few ways to convert the ThroughAssociation to an EntityCollection, but when I do the changes do not get posted back to the database on SaveChanges. Is there a good way to be able to find and modify an entity using ThroughAssociation as coded above? |
|
|
Since Users is a ThroughAssociation it (and its underlying one-to-many association) must be fully loaded before it is used, and you should therefore be able to use LINQ to Objects: User user = groupFound.Users.Where(u => u.UserName == "Test User").First(); You can then modify user as required and any changes should be saved. Note that how you get to a User object -- via a direct Find, a backreference, a child collection or a ThroughAssociation -- should not affect whether it gets saved. If you still see problems saving with the above, your problem may be that the association is being lazily loaded and that this is somehow causing the User not to get associated with the unit of work. Are the underlying associations marked as EagerLoad? If not, could you post your code for declaring and initialising the ThroughAssociation? Thanks! |
|
|
The LINQ to Objects expression you suggested worked to return my entity. It's still not posting back to the database on SaveChanges. Here's some code. In the unit test code below, the Name property shows as "Test User Changed 2" when I break on the final SaveChanges. At the end of the execution, the database shows "Test User". ThroughAssociation: public ThroughAssociation<UserGroup, User> Users { get { return _users; } }[EagerLoad] private ThroughAssociation<UserGroup, User> _users; Unit Test: Application application = new Application("Test Application");Repository.Add(application); Group group = new Group("New Group");User user = new User("Test User");group.Users.Add(user); application.Groups.Add(group); Repository.SaveChanges(); Group groupFound = application.Groups.Find(Group.ByGroupName("New Group"));UserGroup usergroupFound = groupFound.UserGroups.Find(UserGroup.ByUserName("Test User")); usergroupFound.User.Name = "Test User Changed";User userx = groupFound.Users.Where(u => u.Name == "Test User Changed").First();userx.Name = "Test User Changed 2";Repository.SaveChanges();
|
|
|
Interesting. In the sample code above, I have Repository.SaveChanges() called twice. If I comment out the first call to save then the database properly reflects the "Test User Changed 2" value. Should I not be able to call SaveChanges on the Unit Of Work multiple times? |
|
|
You can call SaveChanges() at any point to flush the outstanding changes to the database, so there should be nothing generally wrong with what you are doing. I have put togethor a quick scenario locally here which looks like it matches up with what you are doing but am not running into any strange behavior. Can you add some logging to your test runner (just attach a Console or Trace logger either programatically or by adding the loggerClass attribute into your LightSpeed context configuration definition) and paste the results of the queries that it is running at each stage during your test execution? Alternatively if you can put togethor a small sample which repro's the behavior you are seeing in your test we can have a look at it here :)
Jeremy |
|