Issue updating entity relationship using stub objects

EDIT: Defining stub objects

I have two types of entities Subscriber and AddOn

Their definition in the data model is below

Subscriber ( #SubscriberID , name, AddOnID)

addon ( #AddOnID , Name)

The AddOnID column in the Subscriber table refers to the AddOnID column in the AddOn table.

I am trying to update the AddOn link of a specific Subscriber object. Let's say I want to change subscriber # 1 AddOn link to AddOn # 5. Here's the code:

    Subscriber subscriber = new Subscriber { SubscriberID = 1};
    AddOn newAddOn = new AddOn { AddOnID = 5};

    using (var context = new TestEntities())
    {
        context.AttachTo("AddOn", newAddOn);
        context.AttachTo("Subscriber", subscriber);

        subscriber.Name = "dummy";
        subscriber.AddOn = newAddOn;

        context.SaveChanges();
    }

      

This throws an exception on the line " context.SaveChanges (); "

A link is added or removed from the FK-Subscriber-AddOn association. With capacity limitations, you must also add or remove the corresponding "Subscriber".

When I comment out the line subscriber .AddOn = newAddOn; , the update operation works fine.

So why can't I just update a referenced property like how to update a link without a reference?


Note. I don't know if this is correct, but it adds " context.Refresh (RefreshMode.StoreWins, subscriber); " OR " context.Refresh (RefreshMode. ClietWins, subscriber); " after the addition statements are made to work.

Why is this behavior?

+2


source to share


2 answers


In EF 3.5 SP1, you cannot change a link (i.e. subscriber .Addon) without knowing the original value. Note that this limitation will disappear in EF 4 if you use FK associations .

Now, most of the time EF hides this extra complexity from you, but not when you use Attach like this.

Here is the code you need:

AddOn newAddOn = new AddOn { AddOnID = 5};
AddOn oldAddOn = new AddOn { AddOnID = 4}; // you need to remember the old id.
Subscriber subscriber = new Subscriber { SubscriberID = 1, AddOn = oldAddOn};

using (var context = new TestEntities())
{
    context.AttachTo("AddOn", newAddOn);
    context.AttachTo("Subscriber", subscriber); // will attach the oldAddOn too

    subscriber.Name = "dummy";
    subscriber.AddOn = newAddOn;

    context.SaveChanges();
}

      

As you can see, we just tell EF about the original relationship and then modify it as before.

This should fix your problem.



As you found that the refresh call also works ... because it grabs the original value for the link from the database for you. The disadvantage of using Refresh is that the request is returned back to the database.

So adding the original value as above just saves that extra query.

Hope it helps

for -Alex-

Entity Framework Team.

For more information see Tip 26 from my EF tips section .

+3


source


I think you are asking how to add a child to another object. So, in your case, you have a parent: subscriber and child: AddOn. If so, and your data model has the correct relationship setup, then you probably need the code:



Subscriber subscriber = new Subscriber { SubscriberID = 1};
AddOn newAddOn = new AddOn { AddOnID = 5};

using (var context = new TestEntities())
{

    context.AttachTo("Subscriber", subscriber);
    subscriber.Addon.Add(newAddOn);

    subscriber.Name = "dummy";

    context.SaveChanges();
}

      

0


source







All Articles