I struggled with a similar problem for several hours today and finally was able to solve it. I'm not sure if this will work for your situation, but worth exploring.
The problem may be caused by the Album property of your Song object, marked virtual . I am not an EF expert, but I don't think he likes this virtual property when initializing your anonymous type. Add a non-virtual property for the album path (but keep the virtual navigation property), for example:
public class Song : IPathHavingEntity { public int Id { get; set; } [Required] public string Path { get; set; } [Required] public virtual Album Album { get; set; } public string AlbumPath { get; set; } [Required] public int TrackNumber { get; set; } }
And then execute AddOrUpdate using this non-virtual property, for example:
context.Songs.AddOrUpdate( s => new { FilePath = s.Path, AlbumPath = s.AlbumPath }, new Song { TrackNumber = 1, Path = "01 Intro.mp3", Album = one });
EF should allow you to add songs in which a given song path and album path does not yet exist. If your Song domain object may have a non-virtual AlbumPath property, this is another question, but that should at least allow you to run your seed method in the form you described.
Troy carlson
source share