序列不包含继承元素错误 [英] Sequence contains no elements error with inheritance

查看:148
本文介绍了序列不包含继承元素错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public abstract class MyBaseClass 
{
public int Id {get;组; }

public int OwnerId {get;组; }
public virtual Org Owner {get;组;

}

public abstract class Party:MyBaseClass
{

}

public class Org: Party
{
public string Name {get;组; }
public DateTime CreationDate {get;组;
}

我的数据上下文有这样的一行:

  public DbSet< Org>组织{get;组; } 

执行这样的查询时

  var orgs = db.Orgs; 

我收到此错误:


多重性在关系
'Org_Owner'中的角色'Org_Owner_Source'中无效。因为从属角色属性不是关键的
属性,所以依赖角色
的多重性的上限必须为'*'。


为了克服这个问题,我添加了以下Fluent API代码:

  modelBuilder.Entity< ; MyBaseClass>()
.HasRequired(e => e.Owner)
.WithMany()
.HasForeignKey(e => e.OwnerId)
.WillCascadeOnDelete假);

这解决了无效的多重性错误,但是我随后运行我的查询,它返回一个序列不包含元素错误。当我检查生成的SQL代码正在击中数据库时,我发现来自'子句的查询'引用了一个不存在的MyBaseClassClasses表而不是Orgs表的预期



运行迁移还会引发序列不包含元素错误。无论如何,我不希望所有从 MyBaseClass 继承的类被非规范化到一个MyBaseClassClasses表中。



问题:


  1. 在这种情况下,流利的api代码是否正确?

  2. 如果没有,什么应该是吗?

  3. 如果是,那么我如何克服序列不包含元素错误?

** * ** 后续修改 * ** > * ** * ** * ** * ** * **



另一种我一直在尝试的方式是删除Fluid Mapping和以下类定义:

  public abstract class MyBaseClass 
{
public int Id {get;组; }

[ForeignKey(Owner)]
public int OwnerId {get;组; }
public virtual Org Owner {get;组;

}

public abstract class Party:MyBaseClass
{

}

public class Org: Party
{
public string Name {get;组; }
public DateTime CreationDate {get;组; }
}

此解决方案会抛出Multiplicity在角色Org_Owner_Source中无效关系Org_Owner,因为从属角色属性不是关键属性,因此从属角色的多重性的上限必须为*。错误。



我能够部分解决问题,因为我发现除了Org之外,从MyBaseClass继承的任何其他类都可以拥有一个延迟加载Owner属性,如果属性直接输入到小孩类中,并输入流畅的api条目。



例如:

  public class MyOtherClass:MyBaseClass 
{
public string Name {get;组; }
public string Whaterver {get;组; }
public virtual Org Owner {get;组;
}

使用流体API条目:

  modelBuilder.Entity< MyOtherClass>()。HasRequired(x => x.Owner).WithMany()。HasForeignKey(x => x.OwnerId)。 WillCascadeOnDelete(真); 


解决方案

这是不正确的流畅映射。通过调用 modelBuilder.Entity< MyBaseClass>(),您正在通知实体框架 MyBaseClass 是一个实体,应该以某种方式映射到数据库。而是使用您要映射的最低级别。



例如:

  modelBuilder.Entity< Org>()。HasRequired(e => e.Owner); 


I have a base class from which almost everything else inherits..

public abstract class MyBaseClass
{
   public int Id { get; set; }

   public int OwnerId{ get; set; }
   public virtual Org Owner{ get; set; }

}

public abstract class Party: MyBaseClass
{

}

public class Org: Party
{
   public string Name { get; set; }
   public DateTime CreationDate { get; set; }
}

My data context has a line like this:

public DbSet<Org> Orgs { get; set; }

When executing a query like this...

var orgs = db.Orgs;

I get this error:

Multiplicity is not valid in Role 'Org_Owner_Source' in relationship 'Org_Owner'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

In order to overcome this problem, I added the following Fluent API code:

    modelBuilder.Entity<MyBaseClass>()
                .HasRequired(e => e.Owner)
                .WithMany()
                .HasForeignKey(e => e.OwnerId)
                .WillCascadeOnDelete(false);

This resolves the Invalid Multiplicity error but when I subsequently run my query, it returns a "Sequence contains no elements error". When I inspect the generated SQL code that is hitting the database, I find that the query's 'from' clause is referencing a non-existent MyBaseClassClasses table instead of the Orgs table as expected.

Running a migration also throws the 'Sequence contains no elements' error. Anyway, I wouldn't want all the classes that inherit from MyBaseClass to be denormalized into a single MyBaseClassClasses table.

Questions:

  1. Is the fluent api code correct in this situation?
  2. If no, what should it be?
  3. If yes, then how can I overcome the 'Sequence contains no elements' error?

***** Follow-up Edit ******************

The other way I have been trying it is with the Fluid Mapping removed and the following class definitions:

public abstract class MyBaseClass
{
   public int Id { get; set; }

   [ForeignKey("Owner")]
   public int OwnerId{ get; set; }
   public virtual Org Owner{ get; set; }

}

public abstract class Party: MyBaseClass
{

}

public class Org: Party
{
   public string Name { get; set; }
   public DateTime CreationDate { get; set; }
}

This solution throws the "Multiplicity is not valid in Role 'Org_Owner_Source' in relationship 'Org_Owner'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'." error.

I was able to partially work around the problem as I discovered that any other class, besides Org, that inherits from MyBaseClass can have a lazy loading Owner attribute if the attribute is entered directly in to child class and a fluid api entry is made.

For example:

public class MyOtherClass: MyBaseClass
{
   public string Name { get; set; }
   public string Whaterver { get; set; }
   public virtual Org Owner{ get; set; }
}

With the fluid API entry:

modelBuilder.Entity<MyOtherClass>().HasRequired(x => x.Owner).WithMany().HasForeignKey(x=>x.OwnerId).WillCascadeOnDelete(true);

解决方案

That is not the correct fluent mapping. By calling modelBuilder.Entity<MyBaseClass>(), you are telling Entity Framework that MyBaseClass is an entity and should somehow map to the database. Instead, use lowest class that you want mapped.

For example:

modelBuilder.Entity<Org>().HasRequired(e => e.Owner);

这篇关于序列不包含继承元素错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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