zoukankan      html  css  js  c++  java
  • 如果使用EntityFramework6链接Mysql

    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:

    表中数据:

  • 相关阅读:
    Java基础--HashMap面试题
    数据结构
    Java基础面试题--单利模式及在多线程情况下的运用
    Java多线程面试题--保证多个线程顺序执行
    从客户端(ASPxFormLayout1$txtRule="<YYYY><MM><DD><XXXX>")中检测到有潜在危险的 Request.Form 值
    关于ASPxComboBox通过ClientInstanceName,js获取不到控件的问题
    未能找到类型或命名空间名List
    Postman中使用post方式调用接口
    用户可能引发报错的另类操作
    关于发布程序之后js文件存在缓存问题
  • 原文地址:https://www.cnblogs.com/haoliansheng/p/5666607.html
Copyright © 2011-2022 走看看