如何指定EF /微风两个导航关系(QUOT;无法确定一个有效的排序")? [英] How to specify two navigation relations for ef/breeze ("Unable to determine a valid ordering")?

查看:89
本文介绍了如何指定EF /微风两个导航关系(QUOT;无法确定一个有效的排序")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所敬重的方式有两种实体监控:

I reverence an entity "monitoring" in two ways:


  • 作为。监控名单的一部分,一个公司

  • 作为活动的(=当前选中)公司

public class Company
  {
    [Key]
    [DataMember]
    public virtual Guid CompanyId { get; set; }

    [DataMember]
    public virtual Guid? MonitoringId { get; set; }

    [DataMember]
    [ForeignKey("MonitoringId")]
    public virtual Monitoring ActiveMonitoring { get; set; }

    [DataMember]   
    public virtual ICollection<Monitoring> Monitorings { get; set; }  
  }







public class Monitoring
  {
    [Key]
    [DataMember]
    public virtual Guid MonitoringId { get; set; }

    [DataMember]
    public virtual Guid CompanyId { get; set; }

    [DataMember]
    [ForeignKey("CompanyId")]   
    public virtual Company Company { get; set; } 
 }

如果我创建了一个新的公司和新的监测添加到列表。监控,我可以把它与微风保存。保存新公司后,我能够重新设置alredy增加的监控作为ActiveMonitoring并保存该公司。

If I create a new Company and add a new Monitoring to the list of monitorings, I am able to save it with breeze. After saving the new Company, I am able to set the alredy added Monitoring as ActiveMonitoring and save the Company again.

不过,如果我尝试添加监视设置为ActiveMonitioring并保存公司只有一次,我得到以下错误:

However, If I try to add the Monitoring and set it as ActiveMonitioring and save the Company only once, I get following error:

无法确定相关的操作有效的排序。
依赖可能存在由于外键约束,型号为
的要求,或存储生成的值。

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

下面例如是JavaScript代码来创建新的公司:

Here is example JavaScript code for creating the new Company:

var company = unitofwork.companyRepository.create();
var monitoring = unitofwork.monitoringRepository.create();

//add monitoring to the list of monitorings
monitoring.company(company);
company.monitorings.push(monitoring);

//set monitoring as active monitoring
company.activeMonitoring(monitoring);

unitofwork.commit();



我用的EntityFramework 6和微风1.5.4

I use EntityFramework 6 and Breeze 1.5.4

我试图用注释InverseProperty,使关系更加明确,但没有成功。我也发现了一些相关的计算器的问题,但我无法找到我的具体问题的解决方案:

I tried to use the annotation "InverseProperty" to make the relations more explicit but did not succeed. I also found some related stackoverflow questions but I could not find a solution for my specific issue:

无法确定对于相关的操作

如何指定从实体点¯x两个导航属性相同的目标的实体,Y'

我的类允许下面的案例:一个监控可以是一个公司的名单的一部分,并为其他公司设置为主动监控。但是,我不想让这种情况:一个监测应只允许是主动监测,如果它也是同样的公司。监控名单的一部分。这是我的问题的根本原因是什么?

My classes allow following case: a Monitoring could be part of a list of one Company and be set as active Monitoring for another Company. However, I do not want to allow that case: a Monitoring should only be allowed to be the active Monitoring if it is also part of the list of monitorings of the same Company. Is this the underlying cause of my issue?

是否需要监控两个引用的公司,例如CompanyForListReference和CompanyForActiveReference?还是有那只有一个CompanyId有效的解决方案?

Does the Monitoring need two references to the Company, e.g. CompanyForListReference and CompanyForActiveReference? Or is there a solution that works with only one CompanyId?

我也想过不节能实例作为主动监控,但只有在。监控列表中,例如主动监测指标IndexOfActiveMonitoring。但是,如果我想访问活动监控,我需要一些额外的代码来查找活动的监控。

I also thought about not saving an instance as active Monitoring, but only the index of the active Monitoring in the list of monitorings, e.g. "IndexOfActiveMonitoring". However, if I would like to access the active Monitoring, I would need some extra code to look up the active Monitoring.

=>什么是贯彻落实的推荐方法从公司监控两个引用?

=> What is the recommended way to implement the two references from Company to Monitoring?

推荐答案

我只能为这个实体框架部分发言,但总的问题是相同。这个问题是因为有命令的一个奇怪的命令你将需要以添加该行运行 - 这是不可能做到这一步。你需要插入一行公司(你可以在同一时间增加。监控列表中),然后调用调用SaveChanges。这将生成的ID为实体,然后就可以使用这些更新相关ID。

I can only speak for the Entity Framework part of this, but the overall problem is the same. This problem arises because there is a strange ordering of commands you would need to run in order to add the row - it is not possible to do this in one step. You need to insert the company row (and you can add the list of monitorings at the same time), then call savechanges. This generates the IDs for the entities, and you can then use these to update the related ID.

EF不够聪明,知道在一个单一实体进行多次操作在这些情况下,您需要使用显式事务,并调用调用SaveChanges多次。随着EF-只,这将是类似这样的内容:

EF is not clever enough to know about doing multiple operations on a single entity, in these cases you need to use explicit transactions and call SaveChanges multiple times. With EF-only, this would be something similar to this:

using (var trans = new TransactionScope())
{
    Company company = new Company();   // ... populate
    company.Monitorings.Add(new Monitoring());
    company.Monitorings.Add(new Monitoring());

    context.Companies.Add(company);
    context.SaveChanges();

    company.ActiveMonitoring = company.Monitorings.First();
    context.SaveChanges();

    trans.Complete()
}

这篇关于如何指定EF /微风两个导航关系(QUOT;无法确定一个有效的排序&QUOT;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆