实体框架 - nTier 中的断开行为

发布于 2024-11-17 01:36:42 字数 1763 浏览 7 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2024-11-24 01:36:43

如果您使用 STE(自我跟踪实体),您的 ManageRecommendation 应如下所示:

public void ManageRecommendation(RecommendationTopic recommendationTopic)
{
    using (Entities context = new Entities())
    {
        context.RecommendationTopics.ApplyChanges(recommendationTopic);
        context.SaveChanges();
    }
}

调用 AddObject 会跳过实体的自我跟踪行为。如果您不使用 STE,则必须迭代所有问题并将其状态更改为未更改

public void ManageRecommendation(RecommendationTopic recommendationTopic)
{
    using (Entities context = new Entities())
    {
        context.RecommendationTopics.AddObject(recommendationTopic);
        foreach (var question in recommendationTopic.Questions)
        {
            context.ObjectStateManager.ChangeObjectState(recommendationTopic, EntityState.Unchanged);
        }
        context.SaveChanges();
    }
}

If you are using STEs (self tracking entities) your ManageRecommendation should look like:

public void ManageRecommendation(RecommendationTopic recommendationTopic)
{
    using (Entities context = new Entities())
    {
        context.RecommendationTopics.ApplyChanges(recommendationTopic);
        context.SaveChanges();
    }
}

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 to Unchanged:

public void ManageRecommendation(RecommendationTopic recommendationTopic)
{
    using (Entities context = new Entities())
    {
        context.RecommendationTopics.AddObject(recommendationTopic);
        foreach (var question in recommendationTopic.Questions)
        {
            context.ObjectStateManager.ChangeObjectState(recommendationTopic, EntityState.Unchanged);
        }
        context.SaveChanges();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文