I need to create some dummy records in the database but when I use the code below to fill the database it doesn't create anything.
I get no errors and I can see that the seed method is running.
I suspect that it has to do with identity and auto increment or something but I'm not quite sure.
I have some recursive tables and relationships going on between tables so how can I handle this in my Seed method?
namespace CalMeser.Data.Sql.Migrations
{
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Diagnostics.Contracts;
using CalMeser.Data.Entities;
[ContractVerification(false)]
internal sealed class Configuration : DbMigrationsConfiguration<DataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(DataContext context)
{
context.Tags.AddOrUpdate(new Tag
{
Name = "Computers",
Sort = 1,
Children = new List<Tag>
{
new Tag
{
Name = "Computer Science",
Sort = 1,
Children = new List<Tag>
{
new Tag
{
Name = "Programming",
Sort = 4
},
new Tag
{
Name = "Security",
Sort = 2
},
new Tag
{
Name = "Algorithms",
Sort = 3
},
new Tag
{
Name = "Data Structures",
Sort = 1
},
}
},
new Tag
{
Name = "Computer Information Systems",
Sort = 2
},
new Tag
{
Name = "Computer Engineering",
Sort = 3
}
}
});
context.Tags.AddOrUpdate(new Tag
{
Name = "Nature",
Sort = 1,
Children = new List<Tag>
{
new Tag
{
Name = "Animals",
Sort = 3,
Children = new List<Tag>
{
new Tag
{
Name = "Cats",
Sort = 1
},
new Tag
{
Name = "Dogs",
Sort = 2
},
new Tag
{
Name = "Fish",
Sort = 3
},
new Tag
{
Name = "Monkeys",
Sort = 4
},
}
},
new Tag
{
Name = "Plants",
Sort = 2,
Children = new List<Tag>
{
new Tag
{
Name = "Mint",
Sort = 1
},
new Tag
{
Name = "Sage",
Sort = 2
},
new Tag
{
Name = "Lime",
Sort = 3
}
}
},
new Tag
{
Name = "Space",
Sort = 1
}
}
});
context.SaveChanges();
}
}
}
Here are the entities that are used in the code above.
namespace CalMeser.Data.Abstractions
{
public abstract class Entity : IEntity
{
public long Id { get; set; }
}
}
namespace CalMeser.Data.Abstractions
{
using System.Collections.Generic;
public abstract class RecursiveEntity<TEntity> : Entity where TEntity : RecursiveEntity<TEntity>
{
public virtual IList<TEntity> Children { get; set; }
public virtual TEntity Parent { get; set; }
public long? ParentId { get; set; }
}
}
namespace CalMeser.Data.Entities
{
using CalMeser.Data.Abstractions;
public class Tag : RecursiveEntity<Tag>
{
public File Image { get; set; }
public string Name { get; set; }
public long Sort { get; set; }
}
}