Thursday 29 September 2016

c# Add migration

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add connectionString="server=localhost;database=Inventory;user id=sa;password=SQLPassword"
         name="ContactsConnection"
         providerName="System.Data.SqlClient"/>
    <add connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\chuanshuoge\Documents\Database1.accdb;
Persist Security Info=False;"
         name="MyCompanyConnection"
         providerName="System.Data.OleDb"/>
  </connectionStrings>
 
</configuration>

----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace code_first_demo
{
    public class Context:DbContext
    {
        public Context() : base("name = ContactsConnection") { }

        public DbSet<Contact> C { get; set; }
    }
}

----------------------------------------------------------------------

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

namespace code_first_demo
{
    public class Contact
    {
        public int Id { get; set; }

        public string first_name { get; set; }

        public string last_name { get; set; }

        public string phone { get; set; }

        public string city { get; set; }
    }
}

-----------------------------------------------------------



namespace code_first_demo.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class AddContactscity : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.Contacts", "city", c => c.String());
        }
        
        public override void Down()
        {
            DropColumn("dbo.Contacts", "city");
        }
    }
}

----------------------------------------------------------

namespace code_first_demo.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Contacts",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        first_name = c.String(),
                        last_name = c.String(),
                        phone = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.Contacts");
        }
    }
}

-----------------------------------------------------

namespace code_first_demo.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<code_first_demo.Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            ContextKey = "code_first_demo.Context";
        }

        protected override void Seed(code_first_demo.Context 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" }
            //    );
            //
        }
    }
}

No comments:

Post a Comment