Thursday 6 October 2016

C# SQL migration seed

namespace CPRG254.Publishing.Data.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using CPRG254.Publishing.Domain;

    internal sealed class Configuration : DbMigrationsConfiguration<CPRG254.Publishing.Data.PublishingContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(CPRG254.Publishing.Data.PublishingContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //

            var As = new Author[]
            {
                new Author {FirstName = "Ken", LastName = "hunter", City ="calgary" },
                new Author {FirstName = "jame", LastName = "smith", City ="edmonton" },
            };

            context.Authors.AddOrUpdate(x => x.LastName, As);

            context.SaveChanges();

            var hunter = context.Authors.SingleOrDefault(x => x.LastName == "hunter");
            var smith = context.Authors.SingleOrDefault(x => x.LastName == "smith");

            var Ps = new publisher[]
            {
                new publisher {name="sun" },
                new publisher {name="earth" },
                new publisher {name="mar" }
            };

            context.Ps.AddOrUpdate(x => x.name, Ps);

            context.SaveChanges();

            var p1 = context.Ps.SingleOrDefault(x => x.name == "sun");
            var p2 = context.Ps.SingleOrDefault(x => x.name == "earth");
            var p3 = context.Ps.SingleOrDefault(x => x.name == "mar");

            var Bs = new Book[]
            {
                new Book {ISBN = "111", Title="aaa", Author = hunter, P=p1 },
                new Book {ISBN = "222", Title="bbb", Author = hunter,P=p2 },
                new Book {ISBN = "333", Title="ccc", Author = smith,P=p3 }
            };

            context.Books.AddOrUpdate(x => x.ISBN, Bs);

            context.SaveChanges();
        }

    }
}

----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPRG254.Publishing.Domain
{
    public class publisher
    {
        public int id { get; set; }

        public string name { get; set; }

        public IList<Book> Bs { get; set; }
    }
}

-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPRG254.Publishing.Domain
{
    public class Book
    {
        public int BookId { get; set; }
        public string Title { get; set; }
        public string ISBN { get; set; }
        public int AuthorId { get; set; }

        //navigational property
        public Author Author { get; set; }  

        public publisher P { get; set; }

        public int PId { get; set; }
    }
}

--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPRG254.Publishing.Domain
{
    public class Author
    {
        public int AuthorId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string City { get; set; }

        //navigational property
        public string full_name
        {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }

        public IList<Book> Books { get; set; }
    }
}

-------------------------------------------------
update domain -> Add-Migrations -> write seed -> Update-Database -> drop database in SQL -> run


No comments:

Post a Comment