mvc连接SqlServer数据库配置

在web.config文件里面配置连接字符串

  <connectionStrings>
    <add name="bang" connectionString="Data Source=localhost;Initial Catalog=bang; User Id=zjc;Password=123456" providerName="System.Data.SqlClient"/>
  </connectionStrings>

 

在DbContext类的继承类里面进行配置

    public class SQLContext:DbContext
    {


        public SQLContext():base("bang")
        {

        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>();
            base.OnModelCreating(modelBuilder);
           
        }
       
    }

1,base中使用连接字符串的name

2,重写OnModelCreating方法,配置要映射的实体类,我这里是“user”

文章转载自:快速开发平台– 云微平台
地址:https://www.hocode.com/

 

posted @ 2022-02-12 16:35  码上的飞鱼  阅读(313)  评论(0)    收藏  举报