如何首次保存需要附加另一个预先存在的实体的实体
我需要将公司实体作为新实体保存到数据库中。 为此,我需要将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我这样做:
I do it like this: