EF一对一的多重性

时间:2012-12-21 01:05:09

标签: entity-framework

我正在使用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:Sequence包含多个匹配元素

我认为它与Id有关,但我只是试图创建所描述的关系和多样性

1 个答案:

答案 0 :(得分: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关联。