0

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; }
    }
}
iam3yal
  • 2,188
  • 4
  • 35
  • 44

1 Answers1

0

Well, I decided to ditch auto increment and moved on to Sequential Guid (COMB) so I can precompute the value and initialize it myself.

Here are few articles and posts that helped me to make this decision.

GUIDs as fast primary keys under multiple databases

What are the performance improvement of Sequential Guid over standard Guid?

ID - Sequential Guid (COMB) Vs. Int Identity, using Entity Framework

Community
  • 1
  • 1
iam3yal
  • 2,188
  • 4
  • 35
  • 44