Frameworks EF:相关实体设置为插入,而不是仅引用添加的实体

Frameworks EF:相关实体设置为插入,而不是仅引用添加的实体,frameworks,entity,relationships,Frameworks,Entity,Relationships,我试图通过我得到的相关问题找到答案,但我没有看到与现在相同的情况。我是这个框架的初学者。 问题是,在DB中,始终引用TopicFeedbackType,但如果我附加实体,则始终插入TopicNavigatedUrl和Product事件。我做错了什么?我将找到的实体附加到正确的实体集名称。实体被附加。我注意到,在p_TopicQuickFb的一个属性被附加的实体更新之前,其EntityKey为null,但是当设置currentNavUrl(例如)时,p_TopicQuickFb的EntityKey

我试图通过我得到的相关问题找到答案,但我没有看到与现在相同的情况。我是这个框架的初学者。 问题是,在DB中,始终引用TopicFeedbackType,但如果我附加实体,则始终插入TopicNavigatedUrl和Product事件。我做错了什么?我将找到的实体附加到正确的实体集名称。实体被附加。我注意到,在p_TopicQuickFb的一个属性被附加的实体更新之前,其EntityKey为null,但是当设置currentNavUrl(例如)时,p_TopicQuickFb的EntityKey不再为null。其值为“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);
                    }
                }
            }
      }
一次又一次地运行,只需插入到


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

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


如果有人想评论它为什么这样工作,那就太好了!:)

我自己找到的。分配实体是错误的。Like:p_TopicQuickFb.TopicFeedbackType=currentType;正确的解决方案是,当实体被附加时,只分配id,并将属性保持为null,如:p_TopicQuickFb.TopicFeedbackTypeID=currentType.id;希望这能对你有所帮助!!
    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);