Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了

Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架

Asp.Net Core 2.0 项目实战(3)NCMVC角色权限管理前端UI预览及下载

Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例

Asp.Net Core 2.0 项目实战(5)Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。

Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例

Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密

Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类

Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例

Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录

Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级

本文目录
1. 摘要
2. Ado.Net数据库操作封装类

3. EF Core数据库操作

4. 总结

1.  摘要

  Asp.Net Core2.0下操作MSSQL数据库,这里介绍两种操作方式,一种是.NET Framework的ADO.NET《Ado.Net百科》,另一种就是Net Core2.0下的一种orm操作EF Core,由于本人习惯Ado.Net编程模式,EF Core涉猎不是很深,推荐网友连接,本文有不写的不到之处欢迎大家批评指正。

2.  Ado.Net数据库操作封装类

  2.1配置文件

    在appsettings.json添加相关配置,配置数据库连接字符串,配置与原来在web.config中基本一致,只是形式略有差异。

 //数据库连接
"ConnectionStrings": {
"SqlDSN": "server=.;uid=sa;pwd=123456;database=NCMVC;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=false;"
}

  2.2SqlParameter参数封装DbParameters类

    以前传sql参数是以下这种,操作不太方便,顺序还不乱,添加修改删除字段代码改动量比较大。

SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.NVarChar,) ,
new SqlParameter("@name", SqlDbType.NVarChar,)
};
parameters[].Value = model.id;
parameters[].Value = model.name;

    封装后在使用实例如下,非常方便实用,还不用在意字段类型,所有处理都在封装类中实现。

DbParameters p = new DbParameters();
p.Add("@id", model.id);
p.Add("@name ", model.name);
 using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text; namespace NC.Core
{
public class DbParameters
{
private List<SqlParameter> li; //构造函数
public DbParameters()
{
li = new List<SqlParameter>();
} //单个参数的构造函数
public DbParameters(string strName, object strValue)
{
li = new List<SqlParameter>();
this.Add(strName, strValue);
} #region ** 属性 **
//长度
public int Length
{
get { return li.Count; }
}
//索引
public SqlParameter this[int k]
{
get
{
if (li.Contains(li[k]))
{
SqlParameter parm = li[k];
return parm;
}
else
{
return null;
}
}
}
#endregion #region ** 添加参数
//添加 Input 类型参数
public void Add(string sName, object sValue)
{
li.Add(new SqlParameter()
{
ParameterName = sName.Trim(),
Value = sValue ?? DBNull.Value,
Direction = ParameterDirection.Input,
});
}
//添加 Output 类型参数
public void AddOut()
{
AddOut("@Result", "int", );
}
public void AddOut(string sName, string sDbType, int iSize)
{
li.Add(new SqlParameter()
{
ParameterName = sName,
SqlDbType = ConvertSqlDbType(sDbType),
Size = iSize,
Direction = ParameterDirection.Output,
});
}
public void AddInputOutput(string sName)
{
li.Add(new SqlParameter()
{
ParameterName = sName,
Direction = ParameterDirection.InputOutput,
});
}
public void AddInputOutput(string sName, string sDbType, int iSize)
{
li.Add(new SqlParameter()
{
ParameterName = sName,
SqlDbType = ConvertSqlDbType(sDbType),
Size = iSize,
Direction = ParameterDirection.InputOutput,
});
}
//输出测试内容
public void Output()
{
//netcore2.0里没有HttpContext后续这里改为日志记录
//System.Web.HttpContext.Current.Response.Write("参数输出:---- <br />"); for (int i = ; i < li.Count; i++)
{
SqlParameter p = li[i];
string pName = p.ParameterName;
string pVal = Convert.ToString(p.Value);
//System.Web.HttpContext.Current.Response.Write(pName + " 的值为: " + pVal + " <br />");
}
}
#endregion #region ** 参数转换函数
//SqlDbType数据类型转换
private SqlDbType ConvertSqlDbType(string strDbType)
{
SqlDbType t = new SqlDbType();
switch (strDbType.Trim().ToLower())
{
case "nvarchar": t = SqlDbType.NVarChar; break;
case "nchar": t = SqlDbType.NChar; break;
case "varchar": t = SqlDbType.VarChar; break;
case "char": t = SqlDbType.Char; break;
case "int": t = SqlDbType.Int; break;
case "datetime": t = SqlDbType.DateTime; break;
case "decimal": t = SqlDbType.Decimal; break;
case "bit": t = SqlDbType.Bit; break;
case "text": t = SqlDbType.Text; break;
case "ntext": t = SqlDbType.NText; break;
case "money": t = SqlDbType.Money; break;
case "float": t = SqlDbType.Float; break;
case "binary": t = SqlDbType.Binary; break;
}
return t;
} #endregion #region ** 清空参数集合
public void Clear()
{
li.Clear();
}
#endregion
}
}

DbParameters封装类

  2.3数据库连接、增删改查操作

    默认只有一个数据库连接,多个数据库连接的话再添加实例就可以了,注意这个类是从net freamwork下老项目直接修改得来,net core下并非所有的方法都有使用过。  增、删、改、查均是SQL语句的命令,所以只要存在能向数据库发送SQL脚本的接口则可以实现,Command,要发送脚本总要知道脚本往哪里发找到了Connection,执行完脚本数据库向我们回发结果总要有一个承载 Reader、 Record。Asp.Net Core下提供的基础方法如下,参考DbHelper类完善你自己的SqlHelper类吧。

 using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using System.Data.SqlClient;
using System.Data;
using System;
using System.Collections;
using System.Reflection; using NC.Common;
namespace NC.Core
{
public class DbHelper
{
public static ILogger Log = UtilLogger<DbHelper>.Log;//日志记录 #region --定义变量--
public string dsn;
//默认实例 : DbCommand.SqlDSN.CraeteSqlDataTable(sql, p);
public static DbHelper SqlDSN { get { return new DbHelper(); } } #endregion #region --构造函数--
/// <summary>
/// 构造函数
/// </summary>
public DbHelper()
{
//dsn = Encrypt.Dec(dsn); //解密
//dsn = Configuration.GetConnectionString("SqlDSN");
dsn = UtilConf.GetConnectionString("SqlDSN");
}
/// <summary>
/// 多数据库
/// </summary>
/// <param name="strDSN"></param>
public DbHelper(string strDSN)
{
Log.LogInformation(strDSN);
//dsn = Configuration.GetConnectionString(strDSN);
dsn = UtilConf.GetConnectionString(strDSN);
}
#endregion #region ** 打开/关闭链接 **
/// <summary>
/// 打开链接
/// </summary>
private void ConnOpen(ref SqlCommand comd)
{
if (comd.Connection.State == ConnectionState.Closed)
comd.Connection.Open();
} /// <summary>
/// 关闭链接
/// </summary>
private void ConnClose(ref SqlCommand comd)
{
if (comd.Connection.State == ConnectionState.Open)
{
comd.Connection.Close();
}
comd.Dispose();
}
#endregion #region ** 创建 SqlCommand 对象
/// <summary>
/// 生成comd对象
/// </summary>
public SqlCommand CreateComd(string spName)
{
try
{
SqlConnection conn = new SqlConnection(dsn);
SqlCommand comd = conn.CreateCommand();
comd.CommandText = spName;
comd.CommandType = CommandType.StoredProcedure; return comd;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->CreateComd(sp) 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
public SqlCommand CreateComd(string spName, DbParameters p)
{
try
{
SqlCommand comd = CreateComd(spName); int len = p.Length;
if (len > )
{
for (int i = ; i < len; i++)
{
comd.Parameters.Add(p[i]);
}
}
return comd;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->CreateComd(sp) 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
public SqlCommand CreateSqlComd(string strSql)
{
try
{
SqlConnection conn = new SqlConnection(dsn);
SqlCommand comd = conn.CreateCommand();
comd.CommandText = strSql;
comd.CommandType = CommandType.Text; return comd;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->CreateSqlComd(s) 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
public SqlCommand CreateSqlComd(string strSql, DbParameters p)
{
try
{
SqlCommand comd = CreateSqlComd(strSql); int len = p.Length;
if (len > )
{
for (int i = ; i < len; i++)
{
comd.Parameters.Add(p[i]);
}
}
return comd;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->CreateSqlcomd(s,p) 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
#endregion #region ** 创建 SqlDataAdapter 对象
/// <summary>
/// 根据存储过程名,生成SqlDataAdapter对象
/// </summary>
public SqlDataAdapter CreateAdapter(string spName)
{
try
{
SqlConnection conn = new SqlConnection(dsn);
SqlDataAdapter comdAdapter = new SqlDataAdapter(spName, conn);
comdAdapter.SelectCommand.CommandType = CommandType.StoredProcedure; return comdAdapter;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->CreateAdapter(s) 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
/// <summary>
/// 根据存储过程名和参数,生成SqlDataAdapter对象
/// </summary>
public SqlDataAdapter CreateAdapter(string spName, DbParameters p)
{
try
{
SqlDataAdapter comdAdapter = CreateAdapter(spName); int len = p.Length;
if (len > )
{
for (int i = ; i < len; i++)
{
comdAdapter.SelectCommand.Parameters.Add(p[i]);
}
} return comdAdapter;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->CreateAdapter(s, p) 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
/// <summary>
/// 根据SQL语句,生成DataAdapter对象
/// </summary>
public SqlDataAdapter CreateSqlAdapter(string strSql)
{
try
{
SqlConnection conn = new SqlConnection(dsn);
SqlDataAdapter apter = new SqlDataAdapter(strSql, conn);
apter.SelectCommand.CommandType = CommandType.Text; return apter;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->CreateSqlAdapter(s) 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
/// <summary>
/// 根据SQL语句和参数,生成DataAdapter对象
/// </summary>
public SqlDataAdapter CreateSqlAdapter(string strSql, DbParameters p)
{
try
{
SqlDataAdapter apter = CreateSqlAdapter(strSql); int len = p.Length;
if (len > )
{
for (int i = ; i < len; i++)
{
apter.SelectCommand.Parameters.Add(p[i]);
}
} return apter;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->CreateSqlAdapter(s,p) 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
#endregion #region ** 创建 DataReader 对象
/// <summary>
/// 根据存储过程生成生SqlDataReader
/// </summary>
public SqlDataReader CreateDataReader(string spName)
{
SqlCommand comd = CreateComd(spName);
return GetDataReader(comd);
}
/// <summary>
/// 根据存储过程和参数生成SqlDataReader
/// </summary>
public SqlDataReader CreateDataReader(string spName, DbParameters p)
{
SqlCommand comd = CreateComd(spName, p);
return GetDataReader(comd);
}
/// <summary>
/// 根据SQL语句生成SqlDataReader
/// </summary>
public SqlDataReader CreateSqlDataReader(string strSql)
{
SqlCommand comd = CreateSqlComd(strSql);
return GetDataReader(comd);
}
/// <summary>
/// 根据SQL语句和参数生成SqlDataReader
/// </summary>
public SqlDataReader CreateSqlDataReader(string strSql, DbParameters p)
{
SqlCommand comd = CreateSqlComd(strSql, p);
return GetDataReader(comd);
} #region - GetDataReader()
//获取DataReader
private SqlDataReader GetDataReader(SqlCommand comd)
{
try
{
ConnOpen(ref comd);
return comd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->GetDataReader() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
#endregion
#endregion #region ** 创建 DataTable 对象
/// <summary>
/// 根据存储过程创建 DataTable
/// </summary>
public DataTable CreateDataTable(string spName)
{
SqlDataAdapter adapter = CreateAdapter(spName);
return GetDataTable(adapter);
}
/// <summary>
/// 根据存储过程和参数创建 DataTable
/// </summary>
public DataTable CreateDataTable(string spName, DbParameters p)
{
SqlDataAdapter adapter = CreateAdapter(spName, p);
return GetDataTable(adapter);
}
/// <summary>
/// 根据SQL语句,创建DataTable
/// </summary>
public DataTable CreateSqlDataTable(string strSql)
{
SqlDataAdapter adapter = CreateSqlAdapter(strSql);
return GetDataTable(adapter);
}
/// <summary>
/// 根据SQL语句和参数,创建DataTable
/// </summary>
public DataTable CreateSqlDataTable(string strSql, DbParameters p)
{
SqlDataAdapter adapter = CreateSqlAdapter(strSql, p);
return GetDataTable(adapter);
} #region - GetDataTable()
private DataTable GetDataTable(SqlDataAdapter adapter)
{
try
{
DataTable dt = new DataTable();
adapter.Fill(dt); return dt;
}
catch (System.Exception ex)
{
Log.LogError("DbCommand->GetSqlDataTable() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
finally
{
if (adapter.SelectCommand.Connection.State == ConnectionState.Open)
{
adapter.SelectCommand.Connection.Close();
}
adapter.Dispose();
}
}
#endregion #endregion #region ** 创建 Scalar 对象
/// <summary>
/// 创建无参数的 Scalar 对象
/// </summary>
public object CreateScalar(string spName)
{
SqlCommand comd = CreateComd(spName);
return GetScalar(comd);
}
/// <summary>
/// 有参数的 Scalar 对象
/// </summary>
public object CreateScalar(string spName, DbParameters p)
{
SqlCommand comd = CreateComd(spName, p);
return GetScalar(comd);
}
/// <summary>
/// 根据SQL语句,创建Scalar对象
/// </summary>
public object CreateSqlScalar(string strSql)
{
SqlCommand comd = CreateSqlComd(strSql);
return GetScalar(comd);
}
/// <summary>
/// 根据SQL语句和参数,创建Scalar对象
/// </summary>
public object CreateSqlScalar(string strSql, DbParameters p)
{
SqlCommand comd = CreateSqlComd(strSql, p);
return GetScalar(comd);
} #region - GetScalar()
private object GetScalar(SqlCommand comd)
{
try
{
ConnOpen(ref comd);
object o = comd.ExecuteScalar();
ConnClose(ref comd); return o;
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->GetScalar() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
#endregion
#endregion #region ** 执行数据库操作 - ToExecute() **
/// <summary>
/// 执行数据库操作
/// </summary>
private int ToExecute(SqlCommand comd)
{
try
{
ConnOpen(ref comd);
int iOk = comd.ExecuteNonQuery();
ConnClose(ref comd);
return iOk;
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->ToExecute() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
} private int ToExecuteInt(SqlCommand comd)
{
try
{
ConnOpen(ref comd);
int iOk = ;
int.TryParse(comd.ExecuteScalar().ToString(), out iOk);
ConnClose(ref comd);
return iOk;
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->ToExecute() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
#endregion #region ** 仅执行,不返回输出参数 **
/// <summary>
/// 根据存储过程执行
/// </summary>
public int Execute(string spName)
{
SqlCommand comd = CreateComd(spName);
return ToExecute(comd);
}
/// <summary>
/// 根据存储过程和参数执行
/// </summary>
public int Execute(string spName, DbParameters p)
{
SqlCommand comd = CreateComd(spName, p);
return ToExecute(comd);
}
/// <summary>
/// 执行sql语句
/// </summary>
public int ExecuteSql(string sql)
{
SqlCommand comd = CreateSqlComd(sql);
return ToExecute(comd);
} /// <summary>
/// 执行带参数的SQL语句
/// </summary>
public int ExecuteSqlInt(string sql, DbParameters p)
{
SqlCommand comd = CreateSqlComd(sql, p);
return ToExecuteInt(comd);
}
public int ExecuteSql(string sql, DbParameters p)
{
SqlCommand comd = CreateSqlComd(sql, p);
return ToExecute(comd);
} #endregion #region ** 执行并返回输出参数 **
/// <summary>
/// 执行并返回输出参数
/// </summary>
public string ExecuteOut(string spName, DbParameters p, string outParamName)
{
SqlCommand comd = CreateComd(spName, p);
//comd.Parameters.Add(new SqlParameter(outParamName, SqlDbType.VarChar, 50));
//comd.Parameters[outParamName].Direction = ParameterDirection.Output; try
{
ConnOpen(ref comd);
comd.ExecuteNonQuery();
object o = comd.Parameters[outParamName].Value;
ConnClose(ref comd); return (o == null) ? "" : o.ToString();
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->ExecuteOut() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
} /// <summary>
/// 执行并返回输出参数:默认输出参数 @Result Varchar(50)
/// </summary>
public string ExecuteOut(string spName, DbParameters p)
{
SqlCommand comd = CreateComd(spName, p);
comd.Parameters.Add(new SqlParameter("@Result", SqlDbType.VarChar, ));
comd.Parameters["@Result"].Direction = ParameterDirection.Output; try
{
ConnOpen(ref comd);
comd.ExecuteNonQuery();
object o = comd.Parameters["@Result"].Value;
ConnClose(ref comd); return (o == null) ? "" : o.ToString();
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->ExecuteOut() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
#endregion #region ** 执行并返回输出参数 **
/// <summary>
/// 执行存储过程,并返回输出参数
/// </summary>
public string ExecuteReturn(string spName, DbParameters p, string retParam)
{
SqlCommand comd = CreateComd(spName, p);
comd.Parameters.Add(new SqlParameter(retParam, SqlDbType.VarChar, ));
comd.Parameters[retParam].Direction = ParameterDirection.ReturnValue; //comd.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4, ParameterDirection.ReturnValue, false, 0, 0,String.Empty, DataRowVersion.Default, null)); try
{
ConnOpen(ref comd);
comd.ExecuteNonQuery();
object o = comd.Parameters[retParam].Value;
ConnClose(ref comd); return (o == null) ? "" : o.ToString();
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->ExecuteReturn() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
public string ExecuteReturn(string spName, DbParameters p)
{
SqlCommand comd = CreateComd(spName, p);
comd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.VarChar, ));
comd.Parameters["ReturnValue"].Direction = ParameterDirection.ReturnValue; //comd.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4, ParameterDirection.ReturnValue, false, 0, 0,String.Empty, DataRowVersion.Default, null)); try
{
ConnOpen(ref comd);
comd.ExecuteNonQuery();
object o = comd.Parameters["ReturnValue"].Value;
ConnClose(ref comd); return (o == null) ? "" : o.ToString();
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->ExecuteReturn() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
/// <summary>
/// 执行Sql语句,并返回返回值
/// </summary>
public string ExecuteSqlReturn(string sql, DbParameters p, string retParam)
{
SqlCommand comd = CreateSqlComd(sql, p);
comd.Parameters.Add(new SqlParameter(retParam, SqlDbType.VarChar, ));
comd.Parameters[retParam].Direction = ParameterDirection.ReturnValue; //comd.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4, ParameterDirection.ReturnValue, false, 0, 0,String.Empty, DataRowVersion.Default, null)); try
{
ConnOpen(ref comd);
comd.ExecuteNonQuery();
object o = comd.Parameters[retParam].Value;
ConnClose(ref comd); return (o == null) ? "" : o.ToString();
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->ExecuteReturn() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
}
/// <summary>
/// 根据Sql语句执行
/// </summary>
public string ExecuteSqlReturn(string sql, DbParameters p)
{
SqlCommand comd = CreateSqlComd(sql, p);
comd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.VarChar, ));
comd.Parameters["ReturnValue"].Direction = ParameterDirection.ReturnValue; //comd.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4, ParameterDirection.ReturnValue, false, 0, 0,String.Empty, DataRowVersion.Default, null)); try
{
ConnOpen(ref comd);
comd.ExecuteNonQuery();
object o = comd.Parameters["ReturnValue"].Value;
ConnClose(ref comd); return (o == null) ? "" : o.ToString();
}
catch (System.Exception ex)
{
ConnClose(ref comd);
Log.LogError("DbCommand->ExecuteReturn() 出错\r\n" + ex.Message);
throw new Exception(ex.Message);
}
} #endregion }
}

DbHelper内容太多,请点击查看详细

  2.4调用实例

    读取DataTable:

    DataTable dt = new dbhelper().CreateSqlDataTable("select * from news ");

    读取单个字段:

    Object o=new dbhelper().CreateSqlScalar(“select title from news”);

    添加/修改删除

    String sql=””;

    DbParameters p = new DbParameters();

  p.Add("@id", id);

    int iRes=new dbhelper().ExecuteSql(sql, p);

    调用实例在《Asp.Net Core 2.0 项目实战(11基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级》中也有用到,权限管理控制抽时间在完善一篇,等项目雏形出来了,开源出来配合代码再分享一下,Ado.Net封住调用实例到时一看便知。

3.  EF Core数据库操作

  目前我了解到的EF Core已经支持大部分主流数据库如:Microsoft SQL Server、

  SQLite、Postgres (Npgsql)、 SQL Server Compact Edition、InMemory (for testing purposes);mysql现在不清楚是否已经支持了。

  我用的是数据库生成model这种方式,也就是DB First。参考

  https://www.cnblogs.com/tianma3798/p/6835400.html,https://www.cnblogs.com/luwenlong/p/7804227.html

  注意Net Core下MVC没NetFreamWork 下MVC管理Model的图形界面,nf下数据里改个字段可以在vs上直接右键重新生成model,现在还不太清楚怎么处理,ef core操作方式与原来也略有不同,现在用ef core的时候感觉比较繁琐。另数据库新增,修改,删除字段后,ef core怎么能快捷操作,有知道的朋友请留言告知,大家共同学习。

4.  总结

  无论技术怎么发展,还是由底层一代一代迭代出来的,基础还是要打好,Net Core2.0下操作数据库,牵扯到的内容太多,很多内容描述不出来,请大家配合代码理解实际动手操作一下,这里只能按心中所想配合项目实例列出重点,以及很多朋友可能会碰到的坑点,这里也当是自己的学习记录,有疑问欢迎留言大家讨论。

Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例的更多相关文章

  1. Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录

    1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...

  2. Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级

    1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...

  3. Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例

    本文目录 1. Net下日志记录 2. NLog的使用     2.1 添加nuget引用NLog.Web.AspNetCore     2.2 配置文件设置     2.3 依赖配置及调用     ...

  4. Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类

    本文目录 1.  前沿 2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装 3.XmlHelper快速操作xml文档 4.Serializatio ...

  5. Asp.Net Core 2.0 项目实战(7)MD5加密、AES&DES对称加解密

    本文目录 1. 摘要 2. MD5加密封装 3. AES的加密.解密 4. DES加密/解密 5. 总结 1.  摘要 C#中常用的一些加密和解密方案,如:md5加密.RSA加密与解密和DES加密等, ...

  6. Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例

    本文目录 1. 摘要 2. Redis配置 3. RedisHelper 4.使用实例 5. 总结 1.  摘要 由于內存存取速度远高于磁盘读取的特性,为了程序效率提高性能,通常会把常用的不常变动的数 ...

  7. Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了

    Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...

  8. Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架

    Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...

  9. Asp.Net Core 2.0 项目实战(3)NCMVC角色权限管理前端UI预览及下载

    Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...

随机推荐

  1. Windows句柄数限制

    设置 GDIProcessHandleQuota项设置GDI句柄数量,默认值为2710(16进制)/10000(10进制),该值的允许范围为 256 ~ 16384 ,将其调整为大于默认的10000的 ...

  2. nmap学习之相关参数列表

    一.TARGET SPECIFICATION: Can pass hostnames, IP addresses, networks, etc. Ex: scanme.nmap.org, micros ...

  3. "ls: cannot access sys/class/ieee80211: No such file or directory" .

    1- Do update and upgrade as always. apt-get update && apt-get upgrade && apt-get dis ...

  4. Nginx Server 配置

    http { include mime.types; // 主模块:实现对配置文件包含的文件设定,可以减少主配置文件的复杂度: default_type application/octet-strea ...

  5. 出现fonts/fontawesome-webfont.woff?v=4.5.0 net::ERR_ABORTED

    虽然网页正常显示和运行,但是有2个字体文件出现404错误. 原因:服务器没有配置MIME类型而已. 1. 在IIS网站中,找打网站对应的MIME类型,双击. 2.能看到此网站对应的MIME类型,点击右 ...

  6. android的五个进程优先级,内存不足时被清理的顺序

    Android操作系统尝试尽可能长时间的保持应用的进程,但当可用内存很低时最终要移走一部分进程.怎样确定那些程序可以运行,那些要被销毁,Android让每一个进程在一个重要级的基础上运行,重要级低的进 ...

  7. Android开发技巧——ViewPager加View情况封装PagerAdapter的实现类

    ViewPager是Android的support库中的一个控件. ViewPager + Fragment的使用,已经有FragmentAdapter的实现可以帮助我们快速进行开发了: ViewPa ...

  8. UML和模式应用4:初始阶段(3)--需求制品之用例模型

    1. 前言 UP开发包括四个阶段:初始阶段.细化阶段.构建阶段.移交阶段: UP每个阶段包括 业务建模.需求.设计等科目: 其中需求科目对应的需求制品包括:设想.业务规则.用例模型.补充性规格说明.词 ...

  9. mac使用influxdb和grafana

    mac使用influxdb和grafana influxdb安装以及配置 brew update brew install influxdb ln -sfv /usr/local/opt/influx ...

  10. 【转】assert预处理宏与预处理变量

    assert assert是一个预处理宏,由预处理器管理而非编译器管理,所以使用时都不用命名空间声明,如果你写成std::assert反而是错的.使用assert需要包含cassert或assert. ...