引用nuget包
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Relational
Oracle.EntityFrameworkCore
其中Oracle.EntityFrameworkCore好像只能3.1.x,Microsoft.EntityFrameworkCore的2个版本要一致,版本不要太高,我用的3.1.6,用最新的5.0会报错很麻烦,所以版本能用就好不要随便升最新的
首先创建自己的DBContext类
public class DBContext : DbContext { public DBContext(DbContextOptions options) : base(options) { } public DBContext() : base() { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseOracle( GetConfig("OracleConn") , b => b.UseOracleSQLCompatibility("12")); //指定数据库版本 } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<model>(entity => { entity.ToTable("FirePremise"); entity.HasNoKey(); entity.Property(e => e. Code).IsRequired(); //列映射 entity.Property(e => e. Code).HasColumnName("CODE"); entity.Property(e => e.Type).HasColumnName("TYPE"); base.OnModelCreating(modelBuilder); } public DbSet<SPMODEL> SPMODEL { get; set; } }
参考https://www.cnblogs.com/xhy0826/p/12608801.html,还可以另一种方法
然后调用
var codePara = new OracleParameter("Code", OracleDbType.Varchar2, ParameterDirection.InputOutput) { Value = Code }; var TypePara = new OracleParameter("Type", OracleDbType.NVarchar2, 20, null, ParameterDirection.Output); var strSQL = "BEGIN SP_ (:Code,:Type); END;"; context.Database.ExecuteSqlRaw(strSQL, codePara, TypePara); Code = codePara.Value; Type = TypePara.Value;
当然还需要个model,对应存储过程各个参数
就这个model,要和entity一一对应 modelBuilder.Entity<model>(entity =>