EntityFramework Code First Demo 更新数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EntityFramework Code First Demo 更新数据库相关的知识,希望对你有一定的参考价值。

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 更新数据库的主要内容,如果未能解决你的问题,请参考以下文章

EntityFramework Code-First 简易教程-------领域类的配置

EntityFramework Code-First 简易教程-------领域类配置之DataAnnotations

EntityFramework Code-First 简易教程-------领域类配置之Fluent API

C#+EntityFramework编程方式详细之Code First 数据迁移

EntityFramework Code First Demo 更新数据库

EntityFramework Code First 模式下使用数据迁移