0

我正在使用 EF5,我需要创建以下关系

  • SavedApplicationQueue 和 SavedApplication 之间的一对一
  • BatchApplicationQueue 和 BatchApplication 之间的一对一
public abstract class BaseEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int Id { get; set; }
}
public abstract class Queue : BaseEntity
{
    public string Status { get; set; }
    public DateTime? LastRun { get; set; }
}
public abstract class Application : BaseEntity
{
    public string ApplicationInhertirot { get; set; }
    public string AnotherApplicationInhertirot { get; set; }
}

public class BatchApplication : Application
{
    public bool BatchAppFlag { get; set; }
    public string BatchAppString { get; set; }
    public virtual BatchApplicationQueue BatchApplicationQueue { get; set; }
}

public class SavedApplication : Application
{
    public bool SavedAppFlag { get; set; }
    public string SavedAppString { get; set; }
    public virtual SavedApplicationQueue SavedApplicationQueue { get; set; }
}

public class SavedApplicationQueue : Queue
{
    [Required]
    [ForeignKey("SavedApplication")]
    public virtual SavedApplication SavedApplication { get; set; }
}

public class BatchApplicationQueue :Queue
{
    [Key]
    [ForeignKey("BatchApplication")]
    public override int Id { get; set; }

    [Required]
    public virtual BatchApplication BatchApplication { get; set; }
}

我的流利程度是

modelBuilder.Entity<Application>()
            .Map<SavedApplication>(m =>
                {
                   m.ToTable("SApplication");
                   m.MapInheritedProperties();
                })
            .Map<BatchApplication>(m =>
                {
                   m.ToTable("BApplication");
                   m.MapInheritedProperties();
                });

modelBuilder.Entity<Queue>()
            .Map<SavedApplicationQueue>(m =>
                {
                  m.ToTable("SavedApplicationQueue");
                  m.MapInheritedProperties();
                })
            .Map<BatchApplicationQueue>(m =>
                {
                    m.ToTable("BatchApplicationQueue");
                    m.MapInheritedProperties();
                });

我遇到的问题是当我尝试创建/添加记录时

测试方法 Ef.Model.Test.UnitTest1.TestMethod1 抛出异常:System.InvalidOperationException:序列包含多个匹配元素

我认为这与身份有关,但我只是想创建所描述的关系和多样性

4

1 回答 1

0

首先,异常(显然)是由于映射。您这样做modelBuilder.Entity<Application>(),然后映射两种不同的类型。正确的 TPT 映射将是:

modelBuilder.Entity<SavedApplication>()
            .Map(m =>
              {
                m.ToTable("SApplication");
                m.MapInheritedProperties();
              });
modelBuilder.Entity<BatchApplication>()
            .Map(m =>
              {
                m.ToTable("BApplication");
                m.MapInheritedProperties();
              });

modelBuilder.Entity<SavedApplicationQueue>()
            .Map(m =>
              {
                m.ToTable("SavedApplicationQueue");
                m.MapInheritedProperties();
              });
modelBuilder.Entity<BatchApplicationQueue>()
            .Map(m =>
              {
                m.ToTable("BatchApplicationQueue");
                m.MapInheritedProperties();
              });

其次,虽然你不问,但是Id的覆盖有点奇怪,没有必要。这样的代码将映射两个 1:1 关联。

于 2012-12-21T20:09:22.257 回答