0

I am using EF5 and I need to create the following relationships

  • one-to-one between SavedApplicationQueue and SavedApplication
  • one-to-one between BatchApplicationQueue and 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; }
}

the fluent I have is

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();
                });

the problem I have is when I try and create/add record

Test method Ef.Model.Test.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: Sequence contains more than one matching element

I think it has something to do with the Id but I am simply trying to create the relationships and multiplicity as described

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
kurasa
  • 5,205
  • 9
  • 38
  • 53
  • You can also accomplish this without all the modelBuilder cruft, using just attributes on your models: http://stackoverflow.com/a/12693879/176877 – Chris Moschini Mar 30 '13 at 09:49

1 Answers1

0

First, the exception (apparently) is due the mapping. You do modelBuilder.Entity<Application>() and then map two different types. A correct TPT mapping would be:

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();
              });

Second, although you don't ask, the override of Id is a bit strange and not necessary. The code as such will map two 1:1 associations.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291