码迷,mamicode.com
首页 > 数据库 > 详细

EntityFramework Code First Demo 更新数据库

时间:2017-02-02 17:36:37      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:控制   support   hot   span   程序包   get   token   des   数据   

1、使用Nuget安装EntityFramework

2、AppConfig文件  

<?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 codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="MyContext" connectionString="Data Source=192.168.0.150;port=3306;Initial Catalog=MyBook;user id=root;password=123123123123123123;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
</configuration>

3、执行代码

public class Destination
        {
            public int DestinationId { get; set; }
            public string Name { get; set; }
            public string Country { get; set; }
            public string Description { get; set; }
            public string Description12345 { get; set; }
            public byte[] Photo { get; set; }
            public List<Lodging> Lodgings { get; set; }
        }

        public class Lodging
        {
            public int LodgingId { get; set; }
            public string Name { get; set; }
            public string Owner { get; set; }
            public bool IsResort { get; set; }
            public bool IsResort1 { get; set; }
            public bool IsResort2 { get; set; }
            public Destination Destination { get; set; }
        }
        public class BreakAwayContext : DbContext
        {
            public BreakAwayContext()
                : base("MyContext")
            {

            }
            public DbSet<Destination> Destinations { get; set; }
            public DbSet<Lodging> Lodgings { get; set; }
        }
        static void Main(string[] args)
        {
            var d = DateTime.Now.Date.ToString("yyyyMM");
            var destination = new Destination
            {
                Country = "Indonesia",
                Description = "EcoTourism at its best in exquisite Bali",
                Name = "Bali"
            };
            using (var context = new BreakAwayContext())
            {
                context.Destinations.Add(destination);
                context.SaveChanges();
            }
            Console.WriteLine("OK");
        }

4、用Nuget程序包管理器控制台更新数据库

Enable-Migrations

Update-Database

5、运行

EntityFramework Code First Demo 更新数据库

标签:控制   support   hot   span   程序包   get   token   des   数据   

原文地址:http://www.cnblogs.com/jiecaoge/p/6361381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!