views:

238

answers:

1

This is a follow-up to the discussion in this post. I am not sure how to ask the follow-up question in a 2-month-old thread; so I apologize if there is a better way.

Anyway, to reiterate I have an ASP.NET MVC Edit form, and one of the fields is a select list. I get the values fine, but I am having trouble to update the primary entity after Post. Obviously, I have the key to the lookup entity, but it seems crazy to have to load all lookups. So, the suggested solution is entity reference

For clarity, let's say I have a customer as main entity, and title (Mr / Mrs / Dr, etc.) as the lookup.

So, the link above suggests the following:

customer.TitleReference.EntityKey = new EntityKey("MyEntities.Titles", "Id", 
    Int32.Parse(formData["personTitle"]);

So far, so good. I assign the entity key (and I see in the debugger that is indeed what I expect). But I can't figure out how to get the new value saved along with other customer fields. I am doing the following:

var originalCustomer = (from c in MyEntities.Customers
    where c.Id = customer.Id select c).first();
MyEntities.ApplyPropertyChanges(originalCustomer.EntityKey.EntitySetName, 
    customer); 

This updates all customer fields, except for lookups. Intuitively, it is (somewhat) understandable, since if I specify originalCustomer.EntityKey.EntitySetName, ApplyPropertyChanges ignores originalCustomer.TitleReference.EntityKey.EntitySetName.

But if I do specify originalCustomer.TitleReference.EntityKey.EntitySetName, runtime complains that the entity is null (which is also understandable, since I didn't assign anything to the entity; only to entity reference.

As is probably obvious, I am going circles around what seems to be quite straightforward situation. However, I can't find any tutorials that cover it (which is strange in itself).

Furthermore, I have a more complex problem... the customer may have multiple addresses and the address has state... hopefully, once I figure out the titles - I can extrapolate.

By the way, the example (customer - title - address) is fictitious; but it models the problem quite well.

+1  A: 

This should work:

var originalCustomer = (from c in MyEntities.Customers
    where c.Id = customer.Id select c).First();

originalCustomer.TitleReference.EntityKey = new EntityKey("MyEntities.Titles", "Id", 
    Int32.Parse(formData["personTitle"]);

context.SaveChanges()
LukLed
I get an error: **The member with identity 'MyEntities' does not exist in the metadata collection. Parameter name: identity **in the EntityKey assignment... Unfortunately, I feel that I am at the point of diminishing return (aka wasting time). I read in several places that EF4 is going to be much better; so I will probably load the lookup objects (which already works) and wait until spring for better solution
Felix
@Felix: You have to provide name in EntityContainerName.EntitySetName (e.g. ShopEntities.TitleSet). Did you provide correct name?
LukLed
I am pretty sure I did... In fact, to avoid even the chance of typo, I typed (in an empty space) MyEntities, and then let Intellisense fill Titles for me, and then just cut'n'paste inside the quotes.Of course, it leaves the possibility that I am typing the *wrong* name - but it is the existing name for sure. So - beats me!
Felix
Is `MyEntities = new MyEntities()` or `MyEntities = new SomethingElseEntities()`? If second, you should write `SomethingElseEntities.TitleSet`.
LukLed
That was it! Now, half of my brain is thinking "I am stupid"; and another is thinking "I think I was scouring both the manual and Google for this and couldn't find anything. Am I doing something very unusual, or I missed something obvious?!
Felix
Sometimes you just have to spend many days solving incredibly easy problem:) I think it happens to everyone.
LukLed