简体   繁体   中英

How to work with an DevArt edml file and start a connection?

I am working with the newest DevArt Oracle version and created a EDML file that connects to my Oracle 12 database and get the models with the db first approach.

I followed this howto: https://www.devart.com/entitydeveloper/docs/

So I have my context and my model auto generated:

    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; }
}

Now I try to get it to work in my main in another project (just a simple console application with a 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();

So now I get the error "Keyword user id not supported". I googled this and I found out that problably EF6 is trying to get a default connection, not an oracle connection with DevArt... I tried to play with the app.config in different ways but it didnt help.

Now I tried to create my own connection with the DevArt.Data.Oracle provider, like shown here:

https://www.devart.com/dotconnect/oracle/articles/tutorial-connection.html

  OracleConnection oc = new OracleConnection();
        oc.ConnectionString = constring2;
        oc.Open();
        var test = oc.ServerVersion;

This works fine, so the connectionstring is okay, but still I can't put these two together. I tried to overload the constructor so I can put in my Connection:

       public KiddataAdminEntities(DbConnection con, bool contextOwnsConnection) 
        : base(con, contextOwnsConnection)
    {

    }

Then I got the error on

         protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        throw new UnintentionalCodeFirstException();
    }

That I should not do that...

If you are using XML mapping with Devart Entity Model (*.edml), try this code:

    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();
    }

Refer to

FYI, you can generate fluent mapping (instead of XML mapping). For this, disable a predefined EntityObject template, enable the DbContext template and set the options:

  • Fluent Mapping=True in the properties of DbContext template
  • Metadata Artifact Processing=Do Not Generate Mapping Files in the properties of EntityContextModel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM