Oracle 序列 EF Core 6.0?
Oracle sequence EF Core 6.0?
我需要从 Oracle 数据库中的 SEQUENCE 获取 NEXTVAL。模型构建器确实有一个
builder.HasSequence("TABLE_SEQ");
但我不知道如何使用它。我能想到的唯一方法是标量执行原始 SQL 来检索下一个值。这是要走的路还是有更好的方法?
我发现有几篇帖子说我应该使用 context.Database.SqlQuery()
,但在我的解决方案中却没有。我是否需要添加库才能获得 EF 6.0 的此功能?
我找到的示例:
示例 1:
public int GetNewCertificateTradeRequestIdentity()
{
using var command = _context.Database.GetDbConnection().CreateCommand();
command.CommandText = "SELECT ts.seq_certificate_trade_request.NEXTVAL FROM DUAL";
_context.Database.OpenConnection();
using var reader = command.ExecuteReader();
reader.Read();
return reader.GetInt32(0);
}
示例 2:
users = await context.Database.SqlQuery<User>("Select * from User", new object[] { }).ToListAsync();
_context.Database.GetDbConnection()
context.Database.SqlQuery<x>
都不见了。我在哪里可以找到它们?
好的,在 EF6 中你有 context.Database.GetDbConnection().CreateCommand()
。使用该命令,您可以对数据库执行查询并接收结果。我还找到了一个从 EF6 元数据获取表名的解决方案,并添加了一个扩展方法来处理它。现在我可以执行以下操作:
private Tijdverantwoording Create(decimal? mdwid, decimal? deelprjid, Datum? date)
{
if (mdwid == null || deelprjid == null || date == null) throw new ArgumentNullException();
Weekstaatstatus weekstaatStatus = _WeekstaatStatusService.GetOrCreate(mdwid.Value, date.Jaarweekcode, WeekStaatStatussen.InBewerking, DateTime.Now);
var tijdverantwoording = new Tijdverantwoording
{
Tijdverantwoordingid = GetId<Tijdverantwoording>(), // <= Generate id
Mdwid = mdwid.Value,
Deelprjid = deelprjid.Value,
Datum = date.DagDatum,
Syncstatus = (decimal)SyncStatuses.InBewerking,
Syncdate = DateTime.Now.Date,
Weekstaatstatusid = weekstaatStatus.Weekstaatstatusid
};
_modelContext.Tijdverantwoordingen.Add(tijdverantwoording);
return tijdverantwoording;
}
用于服务的基础 class。
using Microsoft.EntityFrameworkCore;
using MyProjects.Core.Extensions;
using MyProjects.Core.Model;
namespace MyProjects.Core.Services
{
public class ServiceBase
{
private ModelContext? _modelContext;
public ServiceBase(ModelContext modelContext)
{
_modelContext = modelContext;
}
public decimal GetId<T>()
where T : class
{
var command = _modelContext.Database.GetDbConnection().CreateCommand();
var tableName = _modelContext.TableName(typeof(T));
command.CommandType = System.Data.CommandType.Text;
command.CommandText = $"SELECT {tableName}_SEQ.NEXTVAL FROM DUAL";
_modelContext.Database.OpenConnection();
try
{
var result = (decimal?)command.ExecuteScalar();
return result.Value;
}
finally
{
_modelContext.Database.CloseConnection();
}
}
}
}
以及扩展方法
using Microsoft.EntityFrameworkCore;
namespace MyProjects.Core.Extensions
{
public static class DatabaseExtensions
{
public static string? TableName(this DbContext context, Type type)
{
var entityType = context.Model.FindEntityType(type);
return entityType?.GetTableName() ?? throw new NullReferenceException($"Can't find name for type {type.Name}");
}
}
}
我需要从 Oracle 数据库中的 SEQUENCE 获取 NEXTVAL。模型构建器确实有一个
builder.HasSequence("TABLE_SEQ");
但我不知道如何使用它。我能想到的唯一方法是标量执行原始 SQL 来检索下一个值。这是要走的路还是有更好的方法?
我发现有几篇帖子说我应该使用 context.Database.SqlQuery()
,但在我的解决方案中却没有。我是否需要添加库才能获得 EF 6.0 的此功能?
我找到的示例:
示例 1:
public int GetNewCertificateTradeRequestIdentity()
{
using var command = _context.Database.GetDbConnection().CreateCommand();
command.CommandText = "SELECT ts.seq_certificate_trade_request.NEXTVAL FROM DUAL";
_context.Database.OpenConnection();
using var reader = command.ExecuteReader();
reader.Read();
return reader.GetInt32(0);
}
示例 2:
users = await context.Database.SqlQuery<User>("Select * from User", new object[] { }).ToListAsync();
_context.Database.GetDbConnection()
context.Database.SqlQuery<x>
都不见了。我在哪里可以找到它们?
好的,在 EF6 中你有 context.Database.GetDbConnection().CreateCommand()
。使用该命令,您可以对数据库执行查询并接收结果。我还找到了一个从 EF6 元数据获取表名的解决方案,并添加了一个扩展方法来处理它。现在我可以执行以下操作:
private Tijdverantwoording Create(decimal? mdwid, decimal? deelprjid, Datum? date)
{
if (mdwid == null || deelprjid == null || date == null) throw new ArgumentNullException();
Weekstaatstatus weekstaatStatus = _WeekstaatStatusService.GetOrCreate(mdwid.Value, date.Jaarweekcode, WeekStaatStatussen.InBewerking, DateTime.Now);
var tijdverantwoording = new Tijdverantwoording
{
Tijdverantwoordingid = GetId<Tijdverantwoording>(), // <= Generate id
Mdwid = mdwid.Value,
Deelprjid = deelprjid.Value,
Datum = date.DagDatum,
Syncstatus = (decimal)SyncStatuses.InBewerking,
Syncdate = DateTime.Now.Date,
Weekstaatstatusid = weekstaatStatus.Weekstaatstatusid
};
_modelContext.Tijdverantwoordingen.Add(tijdverantwoording);
return tijdverantwoording;
}
用于服务的基础 class。
using Microsoft.EntityFrameworkCore;
using MyProjects.Core.Extensions;
using MyProjects.Core.Model;
namespace MyProjects.Core.Services
{
public class ServiceBase
{
private ModelContext? _modelContext;
public ServiceBase(ModelContext modelContext)
{
_modelContext = modelContext;
}
public decimal GetId<T>()
where T : class
{
var command = _modelContext.Database.GetDbConnection().CreateCommand();
var tableName = _modelContext.TableName(typeof(T));
command.CommandType = System.Data.CommandType.Text;
command.CommandText = $"SELECT {tableName}_SEQ.NEXTVAL FROM DUAL";
_modelContext.Database.OpenConnection();
try
{
var result = (decimal?)command.ExecuteScalar();
return result.Value;
}
finally
{
_modelContext.Database.CloseConnection();
}
}
}
}
以及扩展方法
using Microsoft.EntityFrameworkCore;
namespace MyProjects.Core.Extensions
{
public static class DatabaseExtensions
{
public static string? TableName(this DbContext context, Type type)
{
var entityType = context.Model.FindEntityType(type);
return entityType?.GetTableName() ?? throw new NullReferenceException($"Can't find name for type {type.Name}");
}
}
}