1

Just so you know, I am using: Visual Studio 2008, .NET 3.5

Campaign is many to one relationship with Campaign_Url, so mutiple campaign can share the same Campaign_Url.

As you can see the follow code:

I find the Campaign entity from context with SelectedCampaignID

            Campaign aCampagin = (from c in context.Campaign
                                  where c.campaign_id == SelectedCampaignID
                                  select c).First();

Find the selected Campaign_Url entity from context with SelectedCampaignUrlID

            CampaignUrl aCampaign_Url = (from c_url in context.CampaignUrlSet
                                         where c_url.campaigin_url_id == SelectedCampaignUrlID
                                         select c_url).First();

Assign the CampaignUrl entity to the Campaign entity

            aCampagin.Campaign_Url = aCampaign_Url;
            context.SaveChanges();

But in database, the Campaign table has a url_id column which has foregin key with CampaignUrl table's CampaignUrlID column.

If I am update with normal SQL, I would just

UPDATE Campaign SET url_id  = SelectedCampaignUrlID WHERE Campaign_ID = SelectedCampaignID;

So it would avoided to search the CampaignUrl entity.

I don't believe I am doing the correct way... What will be the correct way of doing this update with entity framework?


EDIT: To include the case I tried to change by ID.

            Campaign aCampagin = (from c in context.Campaign.Include("Campaign_Url")
                                  where c.campaign_id == cam.campaign_id
                                  select c).First();
            aCampagin.Campaign_Url.campaigin_url_id = SelectedCampaignUrlID.Value;
            context.SaveChanges();

It gives error:

The property 'campaigin_url_id' is part of the object's key information and cannot be modified.

King Chan
  • 4,212
  • 10
  • 46
  • 78

3 Answers3

1

The Entity Framework in .NET 3.5 doesn't let you directly set foreign keys.

See the second example here.

I think you need to do something like this:

EntityKey campaignUrlId = 
    new EntityKey("Context.CampaignUrlSet", "campaign_url_id", SelectedCampaignUrlId);

Then you should be able to access the relationship by key instead of by entity.

James's answer is good too.

Edit:

See this question: Entity Framework: Setting a Foreign Key Property

Community
  • 1
  • 1
qxn
  • 17,162
  • 3
  • 49
  • 72
  • Thanks, it worked~ Didn't know it should be **new EntityKey("EntityContainerName.EntityName")** that's why I keep on getting identifier "Context" is missing execption lol... – King Chan Feb 03 '12 at 19:19
0

Long and short answer, yes, or at least that is how I would do it using the entity framework. Though I might handle it like this:

Campaign aCampagin = (from c in context.Campaign
                      where c.campaign_id == SelectedCampaignID
                      select c).First();

aCampagin.Campaign_URL = (from c_url in context.CampaignUrlSet
                          where c_url.campaigin_url_id == SelectedCampaignUrlID
                          select c_url).First();

context.SaveChanges();

The entity framework handles the SQL query that you've posted.

bebleo
  • 326
  • 4
  • 12
  • Yes, because the context.Campaign_UrlSet is the complete set of CampaignUrls. If you needed to assign a new Campaign_URL to a campaign you could: `Campaign_URL aCampaign_URL = new Campaign_URL(); aCampagin.Campaign_URL = aCampaign_URL;` The Id property of aCampaign_URL would be 0 until you called `context.SaveChanges` – bebleo Feb 03 '12 at 18:42
  • One problem, therefore, with working with Ids in the entity framework is that you could potentially pull all entities added to Campaign_Urls since you last saved changes and you would not necessarily know which you were assigning against. – bebleo Feb 03 '12 at 19:01
0

Changing the id directly works just fine in EF.

Or you can Detach() the Campaign from its Campaign_Url and then Attach() it to a different one.

(Technically, you'd detach it from Campaign_Url.Campaigns or whatever the property is for the list of campaigns).

EDIT: Okay, this has already been answered, but none of the answers (including mine) actually cover what I was thinking, so I'm gonna try again:

Instead of

 aCampagin.Campaign_Url = aCampaign_Url;

I believe what they intend you to do is something like this:

aCampagin.Campaign_Url.Campaigns.Remove(aCampaign);
aCampaign_Url.Campaigns.Add(aCampaign);

I tested something like this in my EF project, although admittedly it's EF4.

Dave
  • 4,375
  • 3
  • 24
  • 30
  • I tried that, but it gives me error "The property 'campaigin_url_id' is part of the object's key information and cannot be modified." So this only work in 4.0? – King Chan Feb 03 '12 at 17:47
  • @KingChan -- Yes, that's something new in 4.0, I think. See my answer and the related question I linked. – qxn Feb 03 '12 at 17:55
  • Hmm. Didn't realize that didn't work anymore. I'll file it away. – Dave Feb 03 '12 at 19:25