.NET CORE 数据库的配置以及使用

.net core 可以使用多种方式操作数据库,下面简单讲诉三种方式,仅供参考。

(1) 如果是从之前的.net framework中迁移到core中,之前framework的配置都

是在webconfig中进行的,现在我们需要在app.config文件中进行配置,下面实例用的是企业库。

<configuration>
  <!--db connection-->
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <dataConfiguration defaultDatabase="数据库名" />
  <connectionStrings>
    <add name="数据库名" connectionString="Data Source=10.1.1.11;Initial Catalog=数据库名;User=sa;Password=P@ssw0rd;" providerName="System.Data.SqlClient" />
  </connectionStrings>
   <runtime>
      <gcServer enabled="true"/>
   </runtime>
</configuration>
(2) 还可以使用官网提供的Microsoft.EntityFrameworkCore进行数据库的使用。
首先在Startup.cs 文件中找到ConfigureServices方法,注入

    // Add framework services. 注入services
    services.AddDbContext<ApplySysContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    //DB
    services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

其次添加ApplicationDbContext类
    public class ApplicationDbContext  : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }
    }
    添加ApplySysContext类
    public class ApplySysContext : DbContext
    {
        public ApplySysContext(DbContextOptions<ApplySysContext> options) : base(options)
        {
        }
        public DbSet<Login> Login { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
        }
    }
	添加ApplicationUser类
	        public class ApplicationUser : IdentityUser
        {
        }
		
	之后在controller将ApplySysContext传入,就可以调用dbset的一下方法了,简单的方法如下:
	
	    /// <summary>
        /// 
        /// </summary>
        /// <param name="model">T</param>
        /// <returns>Boolean</returns>
	    public Boolean Insert<T>(T entity) where T : class 
        {
            try
            {
                _db.Set<T>().Add(entity);
                _db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model">T</param>
        /// <returns>Boolean</returns>
        public Boolean BatchInsert<T>(List<T> entityList) where T : class
        {
            try
            {
                foreach (T entity in entityList)
                {
                    _db.Set<T>().Add(entity);
                }
                _db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model">T</param>
        /// <returns>Boolean</returns>
        public Boolean Update<T>(T entity) where T : class
        {
            try
            {
                _db.Set<T>().Update(entity);
                _db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model">T</param>
        /// <returns>Boolean</returns>
        public Boolean Delete<T>(T entity) where T : class
        {
            try
            {
                _db.Set<T>().Remove(entity);
                _db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model">T</param>
        /// <returns>Boolean</returns>
        public T FindByPK<T>(object[] key) where T : class
        {
            try
            {
                return _db.Set<T>().Find(key);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

猜你喜欢

转载自blog.csdn.net/u012601647/article/details/68560994