如何首次保存需要附加另一个预先存在的实体的实体

发布于 2024-07-15 17:52:33 字数 1773 浏览 7 评论 0原文

我需要将公司实体作为新实体保存到数据库中。 为此,我需要将 CompanyType 实体关联到 Company 实体。 如果不这样做我就无法拯救公司。 我是否将新创建的公司附加到上下文,然后将公司类型实体附加到公司,然后保存我的结果? 这段代码可以工作......但就是感觉不对。

public RepositoryStatus SaveCompany(Company company, CompanyType companyType)
    {
        RepositoryStatus rs = new RepositoryStatus();
        try
        {
            using (RanchBuddyEntities dc = new Connection().GetContext())
            {
                if (company.CompanyID > 0)
                {
                    company.UpdateDate = DateTime.Now;
                    Company original = dc.CompanySet.Where(c => c.CompanyID == company.CompanyID).FirstOrDefault();
                    dc.ApplyPropertyChanges("CompanySet", company);
                    dc.Attach(companyType);
                    company.CompanyTypesReference.Attach(companyType);
                }
                else
                {
                    company.CreateDate = DateTime.Now;
                    company.UpdateDate = DateTime.Now;
                    dc.AddToCompanySet(company);
                    dc.Attach(companyType);
                    company.CompanyTypesReference.Value = companyType;
                }
                dc.SaveChanges();
            }
            rs.SetObject(company);
            rs.StatusType = Status.StatusTypes.Success;
        }
        catch (Exception e)
        {
            rs.SetObject(company);
            rs.StatusType = Status.StatusTypes.Failure;
            rs.AddMessage(e.Message + Environment.NewLine + e.StackTrace);
        }

        return rs;
    }

CompanyType 附加到另一个上下文并分离,这就是它在此处传递的原因。 我最好传入 CompanyTypeID 并在同一 using 语句中查询关联对象吗?

我无法忍受我必须采取这么多步骤才能让 EF 变得如此简单,而 LINQ to SQL 如此简单! 尽管如此 - EF 似乎比 NHibernate 更容易!

I need to save a Company entity to the database as a new entity. In order to do this I need to associate a CompanyType entity to the Company entity. I can't save the Company without do this. Do I attach the newly created company to the context and then attach the company type entity to the company and then save my results? This code works...but just doesn't feel right.

public RepositoryStatus SaveCompany(Company company, CompanyType companyType)
    {
        RepositoryStatus rs = new RepositoryStatus();
        try
        {
            using (RanchBuddyEntities dc = new Connection().GetContext())
            {
                if (company.CompanyID > 0)
                {
                    company.UpdateDate = DateTime.Now;
                    Company original = dc.CompanySet.Where(c => c.CompanyID == company.CompanyID).FirstOrDefault();
                    dc.ApplyPropertyChanges("CompanySet", company);
                    dc.Attach(companyType);
                    company.CompanyTypesReference.Attach(companyType);
                }
                else
                {
                    company.CreateDate = DateTime.Now;
                    company.UpdateDate = DateTime.Now;
                    dc.AddToCompanySet(company);
                    dc.Attach(companyType);
                    company.CompanyTypesReference.Value = companyType;
                }
                dc.SaveChanges();
            }
            rs.SetObject(company);
            rs.StatusType = Status.StatusTypes.Success;
        }
        catch (Exception e)
        {
            rs.SetObject(company);
            rs.StatusType = Status.StatusTypes.Failure;
            rs.AddMessage(e.Message + Environment.NewLine + e.StackTrace);
        }

        return rs;
    }

The CompanyType was attached to another context and detached which is why it is passed in here. Would I be better off passing in the CompanyTypeID and query for the associated object inside of the same using statement?

I can't stand that I have to take so many steps to get EF goign where LINQ to SQL was so easy! Still - EF seems easier than NHibernate!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烟雨凡馨 2024-07-22 17:52:33

我这样做:

Company company = new Company();
company.CreateDate = DateTime.Now;
company.UpdateDate = DateTime.Now;
company.CompanyType = companyType;
RanchBuddyEntities.AddToCompanies(company);
RanchBuddyEntities.SaveChanges();

I do it like this:

Company company = new Company();
company.CreateDate = DateTime.Now;
company.UpdateDate = DateTime.Now;
company.CompanyType = companyType;
RanchBuddyEntities.AddToCompanies(company);
RanchBuddyEntities.SaveChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文