zoukankan      html  css  js  c++  java
  • 使用EntityFramework6连接MySql数据库(code first方式)

    demo托管地址:http://git.oschina.net/uustudy/ASP.NET-CodeFirst-MySQL-Demo.git

    之前的是db first(地址:http://www.cnblogs.com/24la/p/ef6-mysql.html

    首先和DB First那篇文章一样,准备工具都要一样的。安装包顺序也是一样的。

    web.config文件中加入这些:

    <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=localhost;port=3306;Initial Catalog=MyBook;user id=root;password=123456;" providerName="MySql.Data.MySqlClient" />
      </connectionStrings>

    新建User类

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    
    namespace CodeFirstMysql
    {
        public class User
        {
            public int Id { get; set; }
            public string UserName { get; set; }
            //默认string映射到mysql里是longtext类型的,加长度之后就变成varchar了
         [MaxLength(
    30)] public string PassWord { get; set; } } }

    新建MyContext类,此类继承DbContext

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;
    
    namespace CodeFirstMysql
    {
        public class MyContext : DbContext
        {
            public MyContext()
                : base("name=MyContext")//web.config中connectionstring的名字
            {
            }
    
            public DbSet<User> Users { get; set; }
        }
    }

    Default.aspx.cs文件内容:

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace CodeFirstMysql
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                InitData();
            }
    
            private void InitData()
            {
                Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
                var context = new MyContext();
           //插入一行值 context.Users.Add(
    new User {UserName = "EF6-MySQL-Code-First"}); context.SaveChanges(); } } }

    运行之后看效果:

    show tables:

    desc table:

    表中数据:

  • 相关阅读:
    第四篇 -- 收获颇丰的一天
    第十六篇 -- SuperIO学习
    第八篇 -- 用U盘制作启动盘装Win10系统
    第二十五篇 -- 学习第三十四天打卡20190726
    zabbix钉钉报警
    windows 远程连接“发生身份验证错误 要求的函数不受支持”
    限制IP远程访问
    查看、踢出在线用户
    codis
    Linux、windows安装java
  • 原文地址:https://www.cnblogs.com/24la/p/ef6-codefirst-mysql.html
Copyright © 2011-2022 走看看