Entity Framework 4 Code Only Error "Multiple objects sets per type are not supported"

10,350

This error occurs if your DbContext class exposes multiple DbSet<T> properties where T occurs more than once. Basically it can't figure out which DbSet an instance of type T belongs to.

In code, the error probably looked like this:

public class MyContex : DbContext {
    public DbSet<PocoA> PocoA { get; set; }
    public DbSet<PocoA> PocoB { get; set; } ...

where that final line should have been DbSet<PocoB> instead of DbSet<PocoA>

TL;DR - you copy-pasted a property and forgot to change the type parameter in the DbSet.

Share:
10,350

Related videos on Youtube

Donald Hughes
Author by

Donald Hughes

Executive technologist with 8+ years of experience in leading IT and software engineering, and 10 years of experience making significant contributions as a software engineer.

Updated on May 01, 2022

Comments

  • Donald Hughes
    Donald Hughes about 2 years

    I have two "Code Only" POCO's using EF4 and the latest CTP, running against an existing, legacy database. Running a LINQ query against PocoA worked until I added the property below to that object, I was trying to add a relationship.

    public virtual PocoB pocoB { get; set; }
    

    Once I did that, I started getting the following error:

    Multiple object sets per type are not supported. The object sets 'PocoA_DbSet' and 'PocoB_DbSet' can both contain instances of type 'PocoA'.

    So I next thought my problem was because I had not defined the relationship, and this legacy database was using a 'fk/pk' prefix instead of an 'Id' suffix on the primary and foreign keys. So I added the following data annotation to the virtual method specified above, with no change in behavior:

    [RelatedTo(Property="PocoB", ForeignKey="fkPocoB")]
    

    I'm really at a loss for what needs to be changed to make this work.

    • Donald Hughes
      Donald Hughes over 13 years
      Found the problem. It was a typo. My DbSet class for PocoB was declared as DbSet<PocoA>.
    • forsvarir
      forsvarir almost 13 years
      You should post your solution as an answer and accept it... That'll take this question off the unanswered list :)
    • Christo
      Christo over 12 years
      agreed, nothing more annoying than getting this far and finding the issue has been resolved. :(
  • J. Mitchell
    J. Mitchell over 11 years
    Thank you for following up. Saved me a headache :)
  • joelmdev
    joelmdev over 10 years
    Just fell victim to the same thing due to copy+paste. This saved me some time.
  • Vijay Chavda
    Vijay Chavda over 7 years
    Same problem. I did the same thing.. copy-paste!