Error in DbContext

Error in DbContext

Postby JoeRuspante » Sun 19 Feb 2012 10:26

Hi everybody.

I have this DataContext:

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Data.Common;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using Devart.Data.Oracle;
using Devart.Data.Oracle.Entity.Configuration;
using System.Data.Objects;
using System.Data.Entity.Infrastructure;
using System.Data;

namespace Test
{

   public class DalContext : DbContext
   {

      public DalContext() : base()
      {
          OracleEntityProviderConfig.Instance.Workarounds.IgnoreSchemaName = true;
      }

      public DalContext(DbConnection conn) : base(conn, true)
      {
          OracleEntityProviderConfig.Instance.Workarounds.IgnoreSchemaName = true;
      }

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove();
          modelBuilder.Conventions.Remove();
          modelBuilder.Conventions.Remove();
            modelBuilder.Entity().ToTable("SYS_Classe0");
            modelBuilder.Entity().HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("ID");
            modelBuilder.Entity().Property(x => x.Descrizione).HasColumnName("DESCRIZIONE").IsRequired().HasMaxLength(250).IsVariableLength().IsUnicode(false).HasColumnType("VARCHAR2");
      }
   }
}


Then I have this simple class:

Code: Select all
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Test
{
   public class Classe0   {
       public virtual long Id { get; set; }
       public virtual string Descrizione { get; set; }
   }


}



Finally, I have this program:

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Devart.Data.Oracle;
using System.Data.Entity;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways());
            using (OracleConnection conn = new OracleConnection())
            {
                conn.AutoCommit = false;
                conn.Direct = true;
                conn.Password = "mypwd";
                conn.Server = "localhost";
                conn.Sid = "XE";
                conn.Unicode = false;
                conn.UserId = "myuser";
                conn.Open();
                OracleCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT COUNT(*) FROM \"SYS_Classe0\"";
                int p = 0;
                using (OracleDataReader r = cmd.ExecuteReader(System.Data.CommandBehavior.SingleResult))
                {
                    while (r.Read())
                    {
                        p = r.GetInt16(0);
                    }
                }
                Console.WriteLine(p);
                Console.ReadLine();
                conn.Close();
            }
            using (OracleConnection conn1 = new OracleConnection())
            {
                conn1.AutoCommit = false;
                conn1.Direct = true;
                conn1.Password = "mypwd";
                conn1.Server = "localhost";
                conn1.Sid = "XE";
                conn1.Unicode = false;
                conn1.UserId = "myuser";
                using (DalContext ctx = new DalContext(conn1))
                {
                    Classe0 c1 = new Classe0();
                    c1.Descrizione = "Prova";
                    ctx.Set().Add(c1);
                    ctx.SaveChanges();
                    var query = from c in ctx.Set()
                                select c.Id;
                    Console.WriteLine(query.Count());
                }
            }
            Console.ReadLine();
        }

    }
}




When I execute the program, the first query is executed correctly (in the console window I see the "0" that means that no record are found). That means that the connection is ok.
But when the "ctx.DbSet()" is executed, I have this error:

Code: Select all
Schema specificato non valido. Errori:
(7,6) : errore 0040: Il tipo nvarchar2 non è qualificato con uno spazio dei nomi o un alias. Solo i tipi PrimitiveType possono essere utilizzati senza qualifica.
(8,6) : errore 0040: Il tipo timestamp non è qualificato con uno spazio dei nomi o un alias. Solo i tipi PrimitiveType possono essere utilizzati senza qualifica.
(9,6) : errore 0040: Il tipo blob non è qualificato con uno spazio dei nomi o un alias. Solo i tipi PrimitiveType possono essere utilizzati senza qualifica.
(10,6) : errore 0040: Il tipo nvarchar2 non è qualificato con uno spazio dei nomi o un alias. Solo i tipi PrimitiveType possono essere utilizzati senza qualifica.


Someone can help me?
JoeRuspante
 
Posts: 54
Joined: Mon 05 Jul 2010 23:08

Postby Shalex » Tue 21 Feb 2012 15:29

There are two ways to solve the problem:

1. If you are not going to use EF Code-First Migrations, we recommend you to remove EntityFramework v 4.3 package from your project and install EntityFramework v 4.2 via Package Manager Console:
Code: Select all
Install-Package EntityFramework -Version 4.2.0.0

2. Stay using EF 4.3. In this case it is necessary to
a) turn on the following options (e.g., in the constructor of the context):
Code: Select all
        var config = OracleEntityProviderConfig.Instance;
        config.Workarounds.IgnoreSchemaName = true;
        config.Workarounds.ColumnTypeCasingConventionCompatibility = true;

b) remove explicit setting of the database type in the mapping:
Code: Select all
        .HasColumnType("VARCHAR2")

We are going to contact Microsoft to resolve the ColumnTypeCasingConvention problems which became more significant because of an implicit creation of an internal DbContext, which is not aware of user's DbContext conventions, within EF engine.
Shalex
Devart Team
 
Posts: 7530
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for Oracle