10

I created a model with 3 classes using table-per-hierarchy inheritance, and with 2 foreign keys that self reference the hierarchy table.

I have a BaseProperty class with a self referencing key CloneID and navigation property Clone:

public class BaseProperty
{
   public int ID {get; set; }

   public int? CloneID {get; set; }

   public BaseProperty Clone {get; set; }

   //If I add this code, when I use Add-Migration - 
   //Sequence contains no elements error
   //public int? TriggeredCloneID {get; set;}
}

I have a Property class that inherits BaseProperty and has a foreign key BlockID and a Block navigation property.

public class Property : BaseProperty
{
   public int? BlockID { get; set; }

   public Block { get; set; }
}

I have a Block class that inherits BaseProperty and has a Properties navigation property:

public class Block: BaseProperty
{  
   public ICollection<Property> Properties { get; set; }

   [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
   public int? ComputedNumberOfProperties { get; set; }
}

Note that the Block property in the Property class is also self referencing because both Property and Block inherit BaseProperty and I am using table-per-hierarchy inheritance.

It has been working and is in a production system with live data. Now I want to add a field (just a standard int? property) to the base class and I get a "Sequence contains no elements" error when I add a migration.

I found the open issue at https://entityframework.codeplex.com/workitem/569, so I tried the workaround described and removed the self-referencing keys and navigation properties, but that migration failed with the same error. I now appear to be completely skewered...

Colin
  • 22,328
  • 17
  • 103
  • 197
  • 1
    Could you post code that is causing that error? – mr100 Apr 23 '15 at 16:04
  • If you don't add any properties at all, and just try creating a new blank migration, does that work as expected? – Chris Curtis Apr 24 '15 at 18:20
  • @ChrisCurtis yes migrations are working normally for changes in other classes. After much trial and error, I've actually managed to get it working. There was also a database computed field in the `Block` class. Removing that (as well as self referencing properties) let me run migrations. I intend to experiment a little with the problem with a view to providing code that replicates the problem. Then I will post it here – Colin Apr 25 '15 at 12:27
  • 1
    Hi @Colin, you can post an answer to your own question for future reference. – Rob Tillie Jul 05 '15 at 08:17

2 Answers2

1

There was also a database computed field in the Block class. Removing that (as well as self referencing properties) let me run a migration.

Colin
  • 22,328
  • 17
  • 103
  • 197
0

I had this issue as well, I tried to change a type from ComicImage to ComicMediaFile... I wasn't careful to change the variable names so I ended up with

List<ComicMediaFile> ComicImages; // and after I changed this variable name to ComicMediaFiles... it worked... I don't know why.
patrick
  • 16,091
  • 29
  • 100
  • 164