0

我试图通过我得到的相关问题找到答案,但我没有看到与现在相同的情况。我是这个框架的初学者。问题是,在数据库中,TopicFeedbackType 总是被引用,但如果我附加实体,TopicNavigatedUrl 和 Product 总是插入事件。我究竟做错了什么?我将找到的实体附加到正确的实体集名称。实体被附加。我注意到在 p_TopicQuickFb 的一个属性被附加实体更新之前,它的 EntityKey 为空,但是当设置 currentNavUrl(例如)时,p_TopicQuickFb 的 EntityKey 不再为空。它的值为“EntitySet=TopicQuickFeedbacks”,但没有 id。我真的迷路了。

    public void AddTopicQuickFeedback(TopicQuickFeedback p_TopicQuickFb, string p_SessionID)
    {
            TopicFeedbackType currentType = this.GetTopicFeedbackType(p_TopicQuickFb.TopicFeedbackType.FeedbackType);
            bool currentTypeAttached = false;

            TopicNavigatedUrl currentNavUrl = this.GetTopicNavigatedUrl(p_TopicQuickFb.TopicNavigatedUrl.Url);
            bool currentNavUrlAttached = false;

            Product currentProduct = this.GetProduct(p_TopicQuickFb.Product.Name, p_TopicQuickFb.Product.MajorVersion, p_TopicQuickFb.Product.MinorVersion);
            bool currentProductAttached = false;

            using (COHFeedbackEntities context = GetObjectContext())
            {
                TopicFeedback tf = GetTopicFeedback(p_SessionID, context);
                if (tf != null)
                {

                    if (currentType != null)
                    {
                        p_TopicQuickFb.TopicFeedbackType = null;
                        context.AttachToOrGet<TopicFeedbackType>("TopicFeedbackTypes", ref currentType);                   
                        currentTypeAttached = true;                            
                        p_TopicQuickFb.TopicFeedbackType = currentType;
                    }

                    if (currentNavUrl != null)
                    {
                        p_TopicQuickFb.TopicNavigatedUrl = null;
                        context.AttachToOrGet<TopicNavigatedUrl>("TopicNavigatedUrls", ref currentNavUrl);                         
                        currentNavUrlAttached = true;  
                        p_TopicQuickFb.TopicNavigatedUrl = currentNavUrl;
                    }

                    if (currentProduct != null)
                    {
                        p_TopicQuickFb.Product = null;
                        context.AttachToOrGet<Product>("Products", ref currentProduct);                  
                        currentProductAttached = true;  
                        p_TopicQuickFb.Product = currentProduct;
                    }

                    tf.TopicQuickFeedbacks.Add(p_TopicQuickFb);
                    context.SaveChanges();
                    context.Detach(tf);

                    if (currentNavUrlAttached)
                    {
                        context.TopicNavigatedUrls.Detach(currentNavUrl);
                    }
                    if (currentProductAttached)
                    {
                        context.Products.Detach(currentProduct);
                    }
                    if (currentTypeAttached)
                    {
                        context.TopicFeedbackTypes.Detach(currentType);
                    }
                }
            }
      }

我在这篇文章中找到了方法:Is is possible to check if an object is already attach to a data context in Entity Framework?

    public static void AttachToOrGet<T>(this System.Data.Objects.ObjectContext context, string entitySetName, ref T entity)
        where T : IEntityWithKey
    {
          System.Data.Objects.ObjectStateEntry entry;
          // Track whether we need to perform an attach
          bool attach = false;
          if (
                context.ObjectStateManager.TryGetObjectStateEntry
                      (
                        context.CreateEntityKey(entitySetName, entity),
                        out entry
                      )
            )
          {
                // Re-attach if necessary
                attach = entry.State == EntityState.Detached;
                // Get the discovered entity to the ref
                entity = (T)entry.Entity;
          }
          else
          {
                // Attach for the first time
                attach = true;
          }
        if (attach)
        {
            context.AttachTo(entitySetName, entity);
        }
    }

测试方法:

        User user = new User(true, false, false);
        string commentStr = "This is my comment";

        Product product = new Product("ProductName", 7, 0);

        TopicFeedbackComment commFeedback = new TopicFeedbackComment(commentStr, new TopicNavigatedUrl("http://testurl.com/test0"), product);

        TopicFeedback feedback = new TopicFeedback(sessionID, user, FeedbackState.New);


        provider.AddTopicFeedback(feedback);

        TopicFeedback addedFeedback = provider.RetrieveTopicFeedback(sessionID);

        provider.AddTopicFeedbackComment(commFeedback, sessionID);

一次又一次地运行它,只需插入到

无法发布图像,因此我可以在必要时提供架构。

4

1 回答 1

0

我的答案在我的最后一条评论中。我自己找到的。

如果有人想评论为什么它会以这种方式工作,那就太好了!:)

于 2013-01-22T13:43:03.277 回答