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.