core版本使用ef连接数据库(一)

时间:2022-05-25 06:21:51

参考

参考代码

sqlserver数据库:①Nuget: Microsoft.EntityFrameworkCore.SqlServer

ORACLE数据库:①Nuget: Oracle.EntityFrameworkCore 

以oracle为列:

新建DataDBContext连接类

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication2
{
    public class DataDBContext : DbContext
    {      
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseOracle(@"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.10.113)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));Persist Security Info=True;User ID=zhzepp;Password=citms", b => b.UseOracleSQLCompatibility("11"));
        }
 
        public DbSet<ASSETSEntity> OrderInfos { get; set; }//实体

       static public void test()
        {
            using (var db = new DataDBContext())
            {
                var count = db.OrderInfos.Where(x => x.ASSETSID != null).ToList();

            }
        }
    }
}

实体类

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication2
{
    [Table("ASSETS")]
    public class ASSETSEntity 
    {
        /// <summary>
        /// 主键
        /// </summary>
        [Key]
        public string ASSETSID { get; set; }
        /// <summary>
        /// TAGS
        /// </summary>
        [Column("TAGS"), MaxLength(2000)]
        public string TAGS { get; set; }
    }
}

在Startup.cs里面

core版本使用ef连接数据库(一)