您的位置:时时app平台注册网站 > 编程知识 > Entityframework 伪CodeFirst开垦形式应用于Sqlite数据库

Entityframework 伪CodeFirst开垦形式应用于Sqlite数据库

2019-11-03 08:45
 public string connectionString = @"DATA SOURCE=oracle11g;USER ID=geovin;password=geovindu;";

时时app平台注册网站 1

using Oracle.DataAccess; //Oracle g 11.2.0
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
//下载 http://www.oracle.com/technetwork/topics/dotnet/downloads/net-downloads-160392.html
//引用:D:appgeovinduproduct11.2.0dbhome_1ODP.NETbin
//用法参考
//
//http://docs.oracle.com/cd/B28359_01/appdev.111/b28844/procedures_dot_net.htm
//http://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleDataAdapterClass.htm //.net 4.0
//https://docs.oracle.com/cd/B19306_01/server.102/b14220/schema.htm

咱俩根本来关爱下那些构造函数,它向程序集 EntityFramework.dll里面,命名空间namespace System.Data.Entity下的DbContext类传递了二个数据库连接字符串的名字。

  System.Data.OleDb

 

引用:

 

 

(摘要:重回一个 System.Data.DataTable,个中包蕴关于落到实处System.Data.Common.DbProviderFactory 的有所已安装提供程序的音信。)

时时app平台注册网站 2

[html] view plaincopy时时app平台注册网站 3时时app平台注册网站 4

ODP.NET:

  1. <connectionStrings>  
  2.     <add name="BloggingContext"   
  3.                connectionString="Data Source=|DataDirectory|Enterprise.sqlite3"  
  4.                providerName="System.Data.SQLite" />  
  5. </connectionStrings>  

  

 

 /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form3_Load(object sender, EventArgs e)
        {
            BindGridView();
        }
        /// <summary>
        /// 
        /// </summary>
        private void BindGridView()
        {
            OracleConnection conn = new OracleConnection(connectionString);
            //ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
            OracleCommand comm = new OracleCommand("pkg_BookKindList.fun_GetRecords", conn);
            comm.Parameters.Add("cur_return", OracleType.Cursor).Direction = ParameterDirection.Output;
            comm.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            using (OracleDataAdapter da = new OracleDataAdapter(comm))
            {

                da.Fill(ds);
            }
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;


        }

 

using System.Collections;
using System.Data;
using System.Configuration;
using System.Data.OracleClient;//.net 4.0

//用法参考
//https://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracledataadapter(v=vs.110).aspx
//http://blog.csdn.net/chinawn/article/details/336904
//C:Program FilesReference AssembliesMicrosoftFramework.NETFrameworkv4.0System.Data.OracleClient.dll

为此大家来布局下当前Web.config文件之中的数据库连接字符串:

  oracle package sql:

 

  

 

Oracle sql:

下边包车型客车安插才是至关重要:

  

表达:以下Sqlite完毕不帮助CodeFirst的(Migration)数据库迁移,数据库是以前创立好的

 C# 3.5 调用查询:

咱俩来看看那一个一连字符串,因为在web下大家得以应用|DataDirectory|来代表根目录下的App_Data文件夹,所以大家把连接字符串写成了地点的款型。

  

先看看数据库上下文类:(以下代码仅供参谋)

---某条记录的字段
drop PROCEDURE proc_Select_BookKindName;

CREATE OR REPLACE PROCEDURE proc_Select_BookKindName(kind_id IN BookKindList.BookKindID%type) AS
    --声明语句段
    v_name varchar2(20);
BEGIN
    --执行语句段
    SELECT o.BookKindName INTO v_name FROM BookKindList o where o.BookKindID=kind_id;
    dbms_output.put_line(v_name);
EXCEPTION
    --异常处理语句段
    WHEN NO_DATA_FOUND THEN dbms_output.put_line('NO_DATA_FOUND');
END;

--测试
begin
proc_Select_BookKindName(1);
end;



---一条记录
--创建包:
create or replace package pack_BookKindId is 
       type cur_BookKindId is ref cursor;  
end pack_BookKindId; 
--创建存储过程
create or replace procedure proc_curBookKindId(p_id in number,p_cur out pack_BookKindId.cur_BookKindId) 
is   
       v_sql varchar2(400);
begin  

       if p_id = 0 then   
          open p_cur for select * from BookKindList; 
       else   
          v_sql := 'select * from BookKindList where BookKindID =: p_id';  
          open p_cur for v_sql using p_id;   
       end if;  
end proc_curBookKindId;


--测试查询一条记录存储过程
-- Test statements here  
set serveroutput on
declare   
 v_id number := 1; --0 时,所有记录 
 v_row BookKindList%rowtype;   --注意这里是表名
 p_cur pack_BookKindId.cur_BookKindId;
begin   
 proc_curBookKindId(v_id, p_cur);  
 loop  
    fetch p_cur into v_row;  
    exit when p_cur%notfound;  
    DBMS_OUTPUT.PUT_LINE(v_row.BookKindName||'='||v_row.BookKindID);  
 end loop;  
 close p_cur;  
end; 

大家先在Web.config文件之中配备如下的配置节,稍后再讲整个的流程是什么运营的。

/// <summary>
    /// 20160918 涂聚文
    /// Geovin Du
    /// </summary>
    public class BookKindListDAL : IBookKindList
    {

        ///<summary>
        /// 追加记录
        ///</summary>
        ///<param name="BookKindListInfo"></param>
        ///<returns></returns>
        public int InsertBookKindList(BookKindListInfo bookKindList)
        {
            int ret = 0;
            try
            {
                OracleParameter[] par = new OracleParameter[]{
    new OracleParameter("temTypeName",OracleDbType.NVarchar2,1000),
    new OracleParameter("temParent",OracleDbType.Int32,4),
    };
                par[0].Value = bookKindList.BookKindName;
                par[1].Value = bookKindList.BookKindParent;
                ret = OracleHelper.ExecuteSql("proc_Insert_BookKindList", CommandType.StoredProcedure, par);
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return ret;
        }
        /// <summary>
        /// 追加记录返回
        /// </summary>
        /// <param name="authorList"></param>
        /// <param name="authorID"></param>
        /// <returns></returns>
        public int InsertBookKindOutput(BookKindListInfo bookKindList, out int bookKindLID)
        {
            bookKindLID = 0;
            int ret = 0;
            try
            {
                OracleParameter[] par = new OracleParameter[]{
    new OracleParameter("temTypeName",OracleDbType.NVarchar2,1000),
                new OracleParameter("temParent",OracleDbType.Int32,4),
                new OracleParameter("temId",OracleDbType.Int32,4),
    };
                par[0].Value = bookKindList.BookKindName;
                par[1].Value = bookKindList.BookKindParent;
                par[2].Direction = ParameterDirection.Output;
                ret = OracleHelper.ExecuteSql("proc_Insert_BookKindOut", CommandType.StoredProcedure, par);
                if (ret > 0)
                {
                    bookKindLID =int.Parse(par[2].Value.ToString());
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return ret;
        }
        ///<summary>
        ///修改记录
        ///</summary>
        ///<param name="BookKindListInfo"></param>
        ///<returns></returns>
        public int UpdateBookKindList(BookKindListInfo bookKindList)
        {
            int ret = 0;
            try
            {
                OracleParameter[] par = new OracleParameter[]{
    new OracleParameter("BookKindID",OracleDbType.Int32,4),
    new OracleParameter("BookKindName",OracleDbType.NVarchar2,1000),
    new OracleParameter("BookKindParent",OracleDbType.Int32,4),
    };
                par[0].Value = bookKindList.BookKindID;
                par[1].Value = bookKindList.BookKindName;
                par[2].Value = bookKindList.BookKindParent;
                ret = OracleHelper.ExecuteSql("proc_Update_BookKindList", CommandType.StoredProcedure, par);
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return ret;
        }
        ///<summary>
        /// 删除记录
        ///</summary>
        ///<param name="bookKindIDInfo"></param>
        ///<returns></returns>
        public bool DeleteBookKindList(int bookKindID)
        {
            bool ret = false;
            try
            {
                OracleParameter par = new OracleParameter("BookKindID", bookKindID);
                int temp = 0;
                temp = OracleHelper.ExecuteSql("proc_Delete_BookKindList", CommandType.StoredProcedure, par);
                if (temp != 0)
                {
                    ret = true;
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return ret;
        }
        ///<summary>
        /// 查询记录
        ///</summary>
        ///<param name="bookKindIDInfo"></param>
        ///<returns></returns>
        public BookKindListInfo SelectBookKindList(int bookKindID)
        {
            BookKindListInfo bookKindList = null;
            try
            {
                OracleParameter par = new OracleParameter("BookKindID", bookKindID);
                using (OracleDataReader reader = OracleHelper.GetReader("proc_Select_BookKindList", CommandType.StoredProcedure, par))
                {
                    if (reader.Read())
                    {
                        bookKindList = new BookKindListInfo();
                        bookKindList.BookKindID = (!object.Equals(reader["BookKindID"], null)) ? (int)reader["BookKindID"] : 0;
                        bookKindList.BookKindName = (!object.Equals(reader["BookKindName"], null)) ? (string)reader["BookKindName"] : "";
                        bookKindList.BookKindParent = (!object.Equals(reader["BookKindParent"], null)) ? (int)reader["BookKindParent"] : 0;

                    }
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return bookKindList;
        }

        ///<summary>
        /// 查询所有记录
        ///</summary>
        ///<returns></returns>
        public List<BookKindListInfo> SelectBookKindListAll()
        {
            List<BookKindListInfo> list = new List<BookKindListInfo>();
            BookKindListInfo bookKindList = null;
            try
            {
                using (OracleDataReader reader = OracleHelper.GetReader("proc_Select_BookKindListAll", CommandType.StoredProcedure, null))
                {
                    while (reader.Read())
                    {
                        bookKindList = new BookKindListInfo();
                        bookKindList.BookKindID = (!object.Equals(reader["BookKindID"], null)) ? (int)reader["BookKindID"] : 0;
                        bookKindList.BookKindName = (!object.Equals(reader["BookKindName"], null)) ? (string)reader["BookKindName"] : "";
                        bookKindList.BookKindParent = (!object.Equals(reader["BookKindParent"], null)) ? (int)reader["BookKindParent"] : 0;
                        list.Add(bookKindList);

                    }
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return list;
        }
        ///<summary>
        /// 查询所有记录
        ///</summary>
        ///<returns></returns>
        public DataTable SelectBookKindListDataTableAll()
        {
            DataTable dt = new DataTable();
            try
            {
                using (DataTable reader = OracleHelper.GetTable("proc_Select_BookKindListAll", CommandType.StoredProcedure, null))
                {
                    dt = reader;


                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return dt;
        }

    }

1.咱们首先创设三个空的WebApplication1

  

然后System.Data.Common下边包车型客车静态类DbProviderFactories的System.Data.Common.DbProviderFactories.GetFactoryClasses()。

    public string connectionString = @"Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = oracle11g)));user id=geovin;password=geovindu;Persist Security Info=True;";

应楼下必要附上德姆o链接:请自行下载(若是对您全体助于,洪福齐天卡塔尔

 string connString = "Provider=OraOLEDB.Oracle.1;User ID=geovin;Password=geovindu;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = oracle11g)))";
            OleDbConnection conn = new OleDbConnection(connString);
            try
            {
                conn.Open();
                MessageBox.Show(conn.State.ToString());
                DataTable dt = conn.GetSchema(this.comboBox1.Text.Trim());
                this.dataGridView1.DataSource = dt;
                this.textBox1.Text = GetColumnNames(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }

/**创建一个名为pkgBookKinds的包查所表中所有内容**/
create or replace package pkg_BookKinds is
--定义一个公有的游标类型cursor_pdt
--ref 可以在程序间传递结果集
--一个程序里打开游标变量,在另外的程序里处理数据
type cursor_pdt is ref cursor;
--声明一个存储过程 ,游标类型参数为输出类型
procedure proc_GetAllBookKind(cur_set out cursor_pdt);
end pkg_BookKinds;

/**创建一个包体**/
create or replace package body pkg_BookKinds is
  --实现包中没有实现的存储过程
  procedure proc_GetAllBookKind(cur_set out cursor_pdt) as
    begin 
      --打开游标,由于定义游标时使用ref处理游标可以推迟到客户端
      open cur_set for select * from BookKindList;
      end;
end;


/**使用过程测试定义的存储过程**/
declare
--定义游标类型的变量
cur_set pkg_BookKinds.cursor_pdt;
--定义行类型
pdtrow BookKindList%rowtype;
begin
  --执行存储过程
  pkg_BookKinds.proc_GetAllBookKind(cur_set);
  --遍历游标中的数据
       LOOP
         --取当前行数据存入pdtrow
           FETCH cur_set INTO pdtrow;
           --如果未获取数据就结束循环
           EXIT WHEN cur_set%NOTFOUND;
           --输出获取到的数据
           DBMS_OUTPUT.PUT_LINE (pdtrow.BookKindID||','||pdtrow.BookKindName);
         END LOOP;
         CLOSE cur_set;
  end;

下一场GetFactory方法通过数据库连接字符串提供的providerName="System.Data.Sqlite",从GetFactoryClasses中找到invariant的值为System.Data.Sqlite的

 

--创建包以游标的形式返回BookKindList的结果集
create or replace package pkg_BookKindList is
-- Author  : geovindu
  type mycur is ref cursor;  
  procedure fun_GetRecords(cur_return out mycur);
end pkg_BookKindList;

create or replace package body pkg_BookKindList is
  -- Function and procedure implementations
 procedure fun_GetRecords(cur_return out mycur)
  is    
  begin
   open cur_return for select * from BookKindList;

  end fun_GetRecords;

end pkg_BookKindList;


declare 
--定义游标类型的变量
cur_return pkg_BookKindList.mycur;
--定义行类型
pdtrow BookKindList%rowtype;
begin
  --执行存储过程
  pkg_BookKindList.fun_GetRecords(cur_return);
  --遍历游标中的数据
       LOOP
         --取当前行数据存入pdtrow
           FETCH cur_return INTO pdtrow;
           --如果未获取数据就结束循环
           EXIT WHEN cur_return%NOTFOUND;
           --输出获取到的数据
           DBMS_OUTPUT.PUT_LINE (pdtrow.BookKindID||','||pdtrow.BookKindName);
         END LOOP;
         CLOSE cur_return;
end;

 

 /// <summary>
        /// 
        /// </summary>
        private void BindGridViewOther()
        {
            OracleConnection conn = new OracleConnection(connectionString);
            //ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
            OracleCommand comm = new OracleCommand("pkg_BookKindList.fun_GetRecords", conn);
            comm.CommandType = CommandType.StoredProcedure;
            //定义参数,注意参数名必须与存储过程定义时一致,且类型为OracleType.Cursor
            OracleParameter cur_set = new OracleParameter("cur_return", OracleType.Cursor);
            //设置参数为输出类型
            cur_set.Direction = ParameterDirection.Output;           
            //添加参数
            comm.Parameters.Add(cur_set);
            DataSet ds = new DataSet();
            using (OracleDataAdapter da = new OracleDataAdapter(comm))
            {

                da.Fill(ds);
            }
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;


        }

在下边包车型大巴:base("Name=BloggingContext")起首化DbContext构造函数的时候,就根据提供的数据库连接字符串找到了,当前的数据库提供程序(当前示范是:System.Data.Sqlite卡塔 尔(阿拉伯语:قطر‎

  

2.我们给当下的工程增加EntityFramework.dll类库,并利用Nuget来保证大家的主次包(Packages),如图所示:

  

SqliteFactory数据工厂(针对Sqlite数据库)

ODP.NET:

只是数额提供程序不周边,也正是providerName属性值钦定了最近的多寡提供程序是哪些。

  ///<summary>
        /// 查询所有记录
        ///</summary>
        ///<returns></returns>
        public List<BookKindListInfo> SelectBookKindListAll()
        {
            List<BookKindListInfo> list = new List<BookKindListInfo>();
            BookKindListInfo bookKindList = null;
            try
            {
                //定义参数,注意参数名必须与存储过程定义时一致,且类型为OracleType.Cursor
                OracleParameter cur_set = new OracleParameter("cur_return", OracleDbType.RefCursor);
                //设置参数为输出类型
                cur_set.Direction = ParameterDirection.Output;
                //OracleHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, "pkg_Select_BookKindListAll.proc_Select_BookKindListAll", cur_set)
                using (OracleDataReader reader = OracleHelper.GetReader("pkg_Select_BookKindListAll.proc_Select_BookKindListAll", CommandType.StoredProcedure, cur_set))
                {
                    while (reader.Read())
                    {
                        bookKindList = new BookKindListInfo();
                        string s = reader["BookKindID"].ToString();
                        bookKindList.BookKindID = (!object.Equals(reader["BookKindID"], null)) ? (decimal)reader["BookKindID"] : 0;
                        bookKindList.BookKindName = (!object.Equals(reader["BookKindName"], null)) ? (string)reader["BookKindName"] : "";
                        bookKindList.BookKindParent = (!object.Equals(reader["BookKindParent"], null)) ? (decimal)reader["BookKindParent"] : 0;
                        list.Add(bookKindList);

                    }
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return list;
        }
        ///<summary>
        /// 查询所有记录
        ///</summary>
        ///<returns></returns>
        public DataTable SelectBookKindListDataTableAll()
        {
            DataTable dt = new DataTable();
            try
            {
                //定义参数,注意参数名必须与存储过程定义时一致,且类型为OracleType.Cursor
                OracleParameter cur_set = new OracleParameter("cur_return", OracleDbType.RefCursor);
                //设置参数为输出类型
                cur_set.Direction = ParameterDirection.Output;
                //添加参数
                //comm.Parameters.Add(cur_set);
                using (DataTable reader = OracleHelper.GetTable("pkg_Select_BookKindListAll.proc_Select_BookKindListAll", CommandType.StoredProcedure, cur_set))
                {
                    dt = reader;


                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return dt;
        }

那般经过反射就会动态创立System.Data.SQLite.SQLiteFactory工厂类了,工厂类成立完毕将来,大家就能够经过工厂类提供的厂子方法,创制Ado.net数据库访谈实例就行数据库访问了。

  数据库连接字符串:

3、大家要把第二步所示的目录上边包车型客车Oracle.DataAccess.dll拷贝到,Web援引程序的主站点上边,也许注册全局程序集缓存(GAC卡塔尔

  System.Data.OracleClient(.net 4.0)

[html] view plaincopy时时app平台注册网站 5时时app平台注册网站 6

引用:

System.Data程序集下边包车型客车System.Data.Common.DbProviderFactoriesConfigurationHandler 管理程序会深入解析<System.data/>配置节

  数据库连接字符串:

4、上述1-3的步子,决意于你的Oracle客商端的本子,因为EntityFramework5.0只好运作在.NetFramework4.0上述,所以大家的Oracle.DataAccess.dll也不得不是4.0的,也正是说D:appAdministratorproduct11.2.0dbhome_1ODP.NETbin  这一个门路下必得有4.0的ODP.NET程序集,不然的话你就要去Oracle的官方网址下载ODAC对应.Net4.0的插件了。

/// <summary>
    /// 20160918 涂聚文
    /// Geovin Du
    /// </summary>
    public class BookKindListDAL : IBookKindList
    {

        ///<summary>
        /// 追加记录
        ///</summary>
        ///<param name="BookKindListInfo"></param>
        ///<returns></returns>
        public int InsertBookKindList(BookKindListInfo bookKindList)
        {
            int ret = 0;
            try
            {
                OracleParameter[] par = new OracleParameter[]{
    new OracleParameter("temTypeName",OracleType.NVarChar,1000),
    new OracleParameter("temParent",OracleType.Number,4),
    };
                par[0].Value = bookKindList.BookKindName;
                par[1].Value = bookKindList.BookKindParent;
                ret = OracleHelper.ExecuteSql("proc_Insert_BookKindList", CommandType.StoredProcedure, par);
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return ret;
        }
        /// <summary>
        /// 追加记录返回
        /// </summary>
        /// <param name="authorList"></param>
        /// <param name="authorID"></param>
        /// <returns></returns>
        public int InsertBookKindOutput(BookKindListInfo bookKindList, out int bookKindLID)
        {
            bookKindLID = 0;
            int ret = 0;
            try
            {
                OracleParameter[] par = new OracleParameter[]{
    new OracleParameter("temTypeName",OracleType.NVarChar,1000),
                new OracleParameter("temParent",OracleType.Number,4),
                new OracleParameter("temId",OracleType.Number,4),
    };
                par[0].Value = bookKindList.BookKindName;
                par[1].Value = bookKindList.BookKindParent;
                par[2].Direction = ParameterDirection.Output;
                ret = OracleHelper.ExecuteSql("proc_Insert_BookKindOut", CommandType.StoredProcedure, par);
                if (ret > 0)
                {
                    bookKindLID =int.Parse(par[2].Value.ToString());
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return ret;
        }
        ///<summary>
        ///修改记录
        ///</summary>
        ///<param name="BookKindListInfo"></param>
        ///<returns></returns>
        public int UpdateBookKindList(BookKindListInfo bookKindList)
        {
            int ret = 0;
            try
            {
                OracleParameter[] par = new OracleParameter[]{
    new OracleParameter("BookKindID",OracleType.Number,4),
    new OracleParameter("BookKindName",OracleType.NVarChar,1000),
    new OracleParameter("BookKindParent",OracleType.Number,4),
    };
                par[0].Value = bookKindList.BookKindID;
                par[1].Value = bookKindList.BookKindName;
                par[2].Value = bookKindList.BookKindParent;
                ret = OracleHelper.ExecuteSql("proc_Update_BookKindList", CommandType.StoredProcedure, par);
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return ret;
        }
        ///<summary>
        /// 删除记录
        ///</summary>
        ///<param name="bookKindIDInfo"></param>
        ///<returns></returns>
        public bool DeleteBookKindList(int bookKindID)
        {
            bool ret = false;
            try
            {
                OracleParameter par = new OracleParameter("BookKindID", bookKindID);
                int temp = 0;
                temp = OracleHelper.ExecuteSql("proc_Delete_BookKindList", CommandType.StoredProcedure, par);
                if (temp != 0)
                {
                    ret = true;
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return ret;
        }
        ///<summary>
        /// 查询记录
        ///</summary>
        ///<param name="bookKindIDInfo"></param>
        ///<returns></returns>
        public BookKindListInfo SelectBookKindList(int bookKindID)
        {
            BookKindListInfo bookKindList = null;
            try
            {
                OracleParameter par = new OracleParameter("BookKindID", bookKindID);
                using (OracleDataReader reader = OracleHelper.GetReader("proc_Select_BookKindList", CommandType.StoredProcedure, par))
                {
                    if (reader.Read())
                    {
                        bookKindList = new BookKindListInfo();
                        bookKindList.BookKindID = (!object.Equals(reader["BookKindID"], null)) ? (int)reader["BookKindID"] : 0;
                        bookKindList.BookKindName = (!object.Equals(reader["BookKindName"], null)) ? (string)reader["BookKindName"] : "";
                        bookKindList.BookKindParent = (!object.Equals(reader["BookKindParent"], null)) ? (int)reader["BookKindParent"] : 0;

                    }
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return bookKindList;
        }

        ///<summary>
        /// 查询所有记录
        ///</summary>
        ///<returns></returns>
        public List<BookKindListInfo> SelectBookKindListAll()
        {
            List<BookKindListInfo> list = new List<BookKindListInfo>();
            BookKindListInfo bookKindList = null;
            try
            {
                using (OracleDataReader reader = OracleHelper.GetReader("proc_Select_BookKindListAll", CommandType.StoredProcedure, null))
                {
                    while (reader.Read())
                    {
                        bookKindList = new BookKindListInfo();
                        bookKindList.BookKindID = (!object.Equals(reader["BookKindID"], null)) ? (int)reader["BookKindID"] : 0;
                        bookKindList.BookKindName = (!object.Equals(reader["BookKindName"], null)) ? (string)reader["BookKindName"] : "";
                        bookKindList.BookKindParent = (!object.Equals(reader["BookKindParent"], null)) ? (int)reader["BookKindParent"] : 0;
                        list.Add(bookKindList);

                    }
                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return list;
        }
        ///<summary>
        /// 查询所有记录
        ///</summary>
        ///<returns></returns>
        public DataTable SelectBookKindListDataTableAll()
        {
            DataTable dt = new DataTable();
            try
            {
                using (DataTable reader = OracleHelper.GetTable("proc_Select_BookKindListAll", CommandType.StoredProcedure, null))
                {
                    dt = reader;


                }
            }
            catch (OracleException ex)
            {
                throw ex;
            }
            return dt;
        }

    }
  1. public partial class BloggingContext : DbContext  
  2. {  
  3.     public BloggingContext(): base("Name=BloggingContext")  
  4.     {  
  5.     }  
  6.     public DbSet<Blog> Blogs { get; set; }  
  7.     public DbSet<Post> Posts { get; set; }  
  8.   
  9.     protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  10.     {  
  11.         modelBuilder.Configurations.Add(new BlogMap());  
  12.         modelBuilder.Configurations.Add(new PostMap());  
  13.     }  
  14. }  

5、同样要配置web.config文件的DbProviderFactorys(不忘记了啊)

[csharp] view plaincopy时时app平台注册网站 7时时app平台注册网站 8

在偏下的求证中自己入眼以创建三个简短的web工程来上课,Sqlite下EntityFramework的伪CodeFirst形式的落到实处。

在这里间安装EntityFramwork的时候本身带上了本子号,因为EntityFramework5用的可比多,所以这里仍旧用老版本好了(二零一六年五月二十四日当前流行版本是6.0卡塔尔。EntityFramework.dll在Nuget上的官方网站地点为:

当中Blog和Post是2个实体类(POCO),这里就不在赘述了。

Oracle数据库连接字符串:

 

        Oracle使用EntityFramework(CodeFirst情势)注意事项        

已设置提供程序的音讯(type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.90.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139")

Oracle数据工厂

[html] view plaincopy时时app平台注册网站 9时时app平台注册网站 10

[html] view plaincopy时时app平台注册网站 11时时app平台注册网站 12

[html] view plaincopy时时app平台注册网站 13时时app平台注册网站 14

世袭会补上类之间的接轨关系。

 

自然大家也能够动用Package Manager Console姣好操作:(Sqlite在Nuget上之处为:)

  1. <system.data>  
  2.        <DbProviderFactories>  
  3.        <add name="SQLite Data Provider" invariant="  
  4.               System.Data.SQLite"   
  5.               description=".NET Framework Data Provider for SQLite"   
  6.               type="System.Data.SQLite.SQLiteFactory,   
  7.               System.Data.SQLite, Version=1.0.90.0, Culture=neutral,   
  8.               PublicKeyToken=db937bc2d44ff139" />  
  9.     </DbProviderFactories>  
  10. </system.data>  
  1. <system.data>  
  2.   <DbProviderFactories>  
  3. Entityframework 伪CodeFirst开垦形式应用于Sqlite数据库时时app平台注册网站。    <add name="Oracle Data Provider for .NET"  
  4.     invariant="Oracle.DataAccess.Client"   
  5.     description="Oracle Data Provider for .NET"  
  6.     type="Oracle.DataAccess.Client.OracleClientFactory,Oracle.DataAccess,  
  7.     Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />  
  8.   </DbProviderFactories>  
  9. </system.data>  

 

1、安装Oracle客商端,暗中认可装在D:app上边(盘符随便)

SqlServer数据库连接字符串:

2、大家看下Oracle自带的ODP.Net组件装在了以下的门径,D:appAdministratorproduct11.2.0dbhome_1ODP.NETbin4.0

 

3.EntityFramework安装好了解后,大家就要加多Sqlite的dll程序集了,笔者那边下载了2个文本直接放在了花色的bin目录下,如下图:

4.至今甘休结束我们着力的配置已经到位了。下边初始遵从CodeFirst形式开创实体模型类和数据上下文类了(DbContext)。

时时app平台注册网站 15

因为近日从辰时间深远的商讨EntityFramework的中间机制,所以实际的得以完结并不丰盛打听。微软早期的初心是付出出后生可畏套通用的数据库访谈逻辑,实现对Dal数据访谈层的惊人封装,其中就用到了工厂情势和浮泛工厂形式。

  1. <connectionStrings>  
  2.   <add name="BloggingContext" connectionString="  
  3.           Data Source=(localdb)v11.0;  
  4.           Initial Catalog=Blogging;Integrated Security=True;  
  5.           MultipleActiveResultSets=True"  
  6.           providerName="System.Data.SqlClient" />  
  7. </connectionStrings>  

 

下边列出2体系型数据库的品种做下相比:

其生龙活虎布局节在C:WindowsMicrosoft.NETFrameworkv4.0.30319ConfigApp.config文件上面也会有,暗许的唯有SqlServer数据库的,数据工厂。

既然EntityFramework要提供风华正茂种通用的数据库访谈逻辑,那么它将要适用于具备的数据库,那么微软是什么样产生的啊?答案是空虚工厂

时时app平台注册网站 16

  1. <connectionStrings>  
  2.   <add name="BloggingContext" connectionString="  
  3.     data source=test;password=123456;user id=sa;"  
  4.     providerName="Oracle.DataAccess.Client" />  
  5. </connectionStrings>  

 

本文由时时app平台注册网站发布于编程知识,转载请注明出处:Entityframework 伪CodeFirst开垦形式应用于Sqlite数据库

关键词: