这是更新与实体框架(.NET 3.5)的多对一关系的正确方法吗?
如您所知,我正在使用:Visual Studio 2008、.NET 3.5
Campaign 与 Campaign_Url 是多对一的关系, 因此多个营销活动可以共享相同的 Campaign_Url。
正如您可以看到以下代码:
我使用 SelectedCampaignID 从上下文中查找 Campaign 实体
Campaign aCampagin = (from c in context.Campaign
where c.campaign_id == SelectedCampaignID
select c).First();
使用 SelectedCampaignUrlID 从上下文中查找选定的 Campaign_Url 实体
CampaignUrl aCampaign_Url = (from c_url in context.CampaignUrlSet
where c_url.campaigin_url_id == SelectedCampaignUrlID
select c_url).First();
将 CampaignUrl 实体分配给 Campaign 实体
aCampagin.Campaign_Url = aCampaign_Url;
context.SaveChanges();
但是在数据库中,Campaign 表有一个 url_id 列,其中的foregin 键为CampaignUrl 表的 CampaignUrlID 列。
如果我使用普通 SQL 进行更新,我会
UPDATE Campaign SET url_id = SelectedCampaignUrlID WHERE Campaign_ID = SelectedCampaignID;
避免搜索 CampaignUrl 实体。
我不相信我正在做正确的方法...使用实体框架进行此更新的正确方法是什么?
编辑:包括我试图通过 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();
它给出错误:
属性“campaigin_url_id”是对象键的一部分 信息且无法修改。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.NET 3.5 中的实体框架不允许您直接设置外键。
请参阅此处的第二个示例。
我认为你需要做这样的事情:
然后你应该能够通过键而不是通过实体访问关系。
詹姆斯的回答也很好。
编辑:
请参阅此问题:实体框架:设置外键属性
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:
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
长话短说,是的,或者至少这就是我使用实体框架的方式。尽管我可能会这样处理:
实体框架处理您发布的 SQL 查询。
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:
The entity framework handles the SQL query that you've posted.
在 EF 中直接更改 id 就可以了。
或者,您可以将营销活动与其 Campaign_Url 分离,然后将其附加到另一个营销活动。
(从技术上讲,您可以将其与 Campaign_Url.Campaigns 或营销活动列表的任何属性分离)。
编辑:好的,这已经得到了回答,但是没有一个答案(包括我的)实际上涵盖了我的想法,所以我要再试一次:
而不是
我相信他们打算你做的是这样的:
我在我的 EF 项目中测试了类似的东西,尽管不可否认它是 EF4。
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
I believe what they intend you to do is something like this:
I tested something like this in my EF project, although admittedly it's EF4.