我正在使用最新的DevArt Oracle版本,并创建了一个EDML文件,该文件连接到我的Oracle 12数据库,并使用数据库优先的方法获取模型。
我遵循以下方法:https://www.devart.com/entitydeveloper/docs/
所以我有我的上下文和我的模型自动生成:
public partial class KiddataAdminEntities : DbContext
{
#region Constructors
/// <summary>
/// Initialize a new KiddataAdminEntities object.
/// </summary>
public KiddataAdminEntities() :
base(@"name=KiddataAdminEntitiesConnectionString")
{
Configure();
}
/// <summary>
/// Initializes a new KiddataAdminEntities object using the connection string found in the 'KiddataAdminEntities' section of the application configuration file.
/// </summary>
public KiddataAdminEntities(string nameOrConnectionString) :
base(nameOrConnectionString)
{
Configure();
}
private void Configure()
{
this.Configuration.AutoDetectChangesEnabled = true;
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = true;
this.Configuration.ValidateOnSaveEnabled = true;
}
#endregion
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Anrede> Anrede { get; set; }
}
现在,我试图让它在另一个项目(只是一个简单的控制台应用程序,带有start.cs(中在我的主体中工作:
KiddataAdminEntities context = new KiddataAdminEntities("User Id=xxxx;Password=xxxx;Server=xx;Direct=True;Sid=xxxx;Persist Security Info=True");
var listOfAnrede = context.Anrede.ToList();
所以现在我得到了错误";关键字用户id不受支持";。我在谷歌上搜索了一下,发现EF6可能试图获得一个默认连接,而不是与DevArt的oracle连接。。。我试着用不同的方式玩app.config,但无济于事。
现在我尝试创建我自己与DevArt.Data.Oracle提供者的连接,如图所示:
https://www.devart.com/dotconnect/oracle/articles/tutorial-connection.html
OracleConnection oc = new OracleConnection();
oc.ConnectionString = constring2;
oc.Open();
var test = oc.ServerVersion;
这很好用,所以连接字符串是可以的,但我仍然不能把这两者放在一起。我试图重载构造函数,这样我就可以把我的连接:
public KiddataAdminEntities(DbConnection con, bool contextOwnsConnection)
: base(con, contextOwnsConnection)
{
}
然后我在上得到了错误
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
我不应该那样做。。。
如果您正在使用Devart实体模型(*.edml(的XML映射,请尝试以下代码:
using Devart.Data.Oracle;
using System.Data.EntityClient;
...
OracleConnectionStringBuilder oracleBuilder = new OracleConnectionStringBuilder();
oracleBuilder.UserId = "...";
oracleBuilder.Password = "...";
oracleBuilder.Server = "...";
oracleBuilder.Direct = true;
oracleBuilder.Sid = "...";
oracleBuilder.PersistSecurityInfo = true;
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "Devart.Data.Oracle";
entityBuilder.ProviderConnectionString = oracleBuilder.ConnectionString;
entityBuilder.Metadata = @"res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl";
using (Entities context = new Entities(entityBuilder.ToString())) {
var a = context.MyEntity.First();
}
参考
- https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/how-to-build-an-entityconnection-connection-string
- https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/connection-strings
FYI,您可以生成流畅的映射(而不是XML映射(。为此,禁用预定义的EntityObject模板,启用DbContext模板并设置选项:
- 在DbContext模板的属性中Fluent Mapping=True
- 元数据项目处理=不在EntityContextModel的属性中生成映射文件