I'm trying to create my first EF code first objects into database. Unfortunately when trying to save my objects into DB I get this exception:
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Here is the code I use to fill the DB:
using (var context = new MySample.Context())
{
context.Database.CreateIfNotExists();
CloudFile f = new CloudFile(, "file1");
CloudDirectory d = new CloudDirectory(CloudObject.CloudContentType.Customer, "directory1");
d.Nodes.Add(d);
FileGroup g = new FileGroup();
g.CloudObjects.Add(d);
context.FileGroups.Add(g);
context.SaveChanges();
}
Actually I would like to create an object of type FileGroup. This object contains either files (CloudFile) or directories (CloudDirectory). The directories can contain either other directories or files.
Here are the classes for the mentioned objects:
public class FileGroup {
public FileGroup()
{
CloudObjects = new HashSet<CloudObject>();
}
public int FileGroupId { get; set; }
public string Name { get; set; }
public virtual ICollection<CloudObject> CloudObjects { get; set; }
}
public abstract class CloudObject {
public CloudObject(string name)
{
Name = name;
}
public int CloudObjectId { get; set; }
public string Name { get; set; }
public abstract bool hasChildren { get; }
public int? ParentId { get; set; }
public virtual Node Parent { get; set; }
public int? FileGroupId { get; set; }
public virtual FileGroup FileGroup { get; set; }
}
public class CloudDirectory : CloudObject {
public CloudDirectory(string name) :base(name)
{
Nodes = new HashSet<CloudObject>();
}
public override bool hasChildren
{
get
{
return Nodes.Any();
}
}
public virtual ICollection<CloudObject> Nodes { get; set; }
}
public class CloudFile : CloudObject {
public CloudFile(string name) : base(name)
{
}
public string ETag { get; set; }
public override bool hasChildren
{
get
{
return false;
}
}
}
Any idea what I need to change in my objects so that they can be stored successfully do DB?
Nodeherepublic virtual Node Parent { get; set; }? - Ivan Stoev