实体框架 - nTier 中的断开行为
我是 EF 新手,但我会尽力描述该场景。我的数据库中有 3 个表,即RecommendationTopic、Recommendation 和 Question。每个推荐主题可以有多个推荐,每个推荐可以有多个问题。假设我的问题表中已经有预定义的问题。
我有一项服务返回如下问题列表:
public List<Question> FetchQuestions(int categoryID)
{
using (Entities context = new Entities())
{
questions = context.Questions.Where(i => i.ID >= 0).ToList();
}
}
我有另一项服务用于创建RecommendationTopic 和Recommendation,其代码如下所示:
public void ManageRecommendation(RecommendationTopic recommendationTopic)
{
using (Entities context = new Entities())
{
context.AddObject("RecommendationTopics", recommendationTopic);
context.SaveChanges();
}
}
我的客户端代码如下所示:
List<Question> questions;
using (QuestionServiceClient client = new QuestionServiceClient())
{
questions = client.FetchQuestions();
}
using (RecommendationServiceClient client = new RecommendationServiceClient())
{
RecommendationTopic rTopic = new RecommendationTopic();
rTopic.CategoryID = 3;
rTopic.Name = "Topic From Client";
Recommendation rCom = new Recommendation();
rCom.Text = "Dont!";
rCom.RecommendationTopic = rTopic;
rCom.ConditionText = "Some condition";
rCom.Questions.Add(questions[0]);
rCom.Questions.Add(questions[1]);
client.ManageRecommendation(rTopic);
}
由于客户端进行了 2 个单独的服务调用,因此上下文两次通话都会有所不同。当我尝试运行它并检查 EF 分析器时,它不仅生成查询以插入到RecommendationTopic 和Recommendation 中,而且还生成问题表!
我确信这是由于两个调用的上下文不同而引起的,因为当我在单个上下文中执行类似的代码时,它会按预期工作。
问题是,如何使其在断开连接的情况下工作?
我的客户端可能是 Silverlight 客户端,我需要通过单独的调用填写问题下拉列表,并在单独的调用中保存推荐主题。因此,我也使用自我跟踪实体。
任何意见表示赞赏! -维诺德
I am new to EF but I will try my best to describe the scenario. I have 3 tables in My DB namely RecommendationTopic, Recommendation and Question. Each RecommendationTopic can have multiple Recommendations and each Recommendation may have multiple questions. Assume that I already have predefined questions in my Question table.
I have one service that returns me list of questions like below:
public List<Question> FetchQuestions(int categoryID)
{
using (Entities context = new Entities())
{
questions = context.Questions.Where(i => i.ID >= 0).ToList();
}
}
I have another service which is used to create RecommendationTopic and Recommendation whose code is something like below:
public void ManageRecommendation(RecommendationTopic recommendationTopic)
{
using (Entities context = new Entities())
{
context.AddObject("RecommendationTopics", recommendationTopic);
context.SaveChanges();
}
}
My client code looks like below:
List<Question> questions;
using (QuestionServiceClient client = new QuestionServiceClient())
{
questions = client.FetchQuestions();
}
using (RecommendationServiceClient client = new RecommendationServiceClient())
{
RecommendationTopic rTopic = new RecommendationTopic();
rTopic.CategoryID = 3;
rTopic.Name = "Topic From Client";
Recommendation rCom = new Recommendation();
rCom.Text = "Dont!";
rCom.RecommendationTopic = rTopic;
rCom.ConditionText = "Some condition";
rCom.Questions.Add(questions[0]);
rCom.Questions.Add(questions[1]);
client.ManageRecommendation(rTopic);
}
Since the client makes 2 separate service calls, the context would be different for both the calls. When I try to run this and check the EF profiler, it not only generates query to insert into RecommendationTopic and Recommendation but also Question table!
I am sure this is caused due to different context for both the calls as when I execute a similar code within a single context, it works as it's supposed to work.
Question is, how do I make it work in a disconnected scenario?
My client could be Silverlight client where I need to fill a Question drop down with a separate call and save Recommendation topic in a separate call. For this reason I am using self tracking entities as well.
Any input appreciated!
-Vinod
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 STE(自我跟踪实体),您的
ManageRecommendation
应如下所示:调用
AddObject
会跳过实体的自我跟踪行为。如果您不使用 STE,则必须迭代所有问题并将其状态更改为未更改
:If you are using STEs (self tracking entities) your
ManageRecommendation
should look like:Calling
AddObject
skips self tracking behavior of your entity. If you are not using STEs you must iterate through all questions and change their state toUnchanged
: