Entity Framework 异常:指定的架构无效。

Entity Framework Exception: Schema specified is not valid.

我在 Asp.net 和 entity framework 在 vs 2010 中使用 oracle 数据库工作。我为实体框架工作数据上下文提供来自代码的连接字符串。这是我的代码:

public static string getConStrSQL()
    {

        //string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
        string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
        {
            Metadata = "res://*",
            Provider = "Oracle.ManagedDataAccess.Client",
            //Provider = "Oracle.DataAccess.Client",
            ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
            {
                //InitialCatalog = "ORCL",
                DataSource = "MONOJ-PC:1521/ORCL",
                //IntegratedSecurity = false,
                UserID = "C##MONOJ",                 // User ID such as "sa"
                Password = "Thanks123",               // hide the password
            }.ConnectionString
        }.ConnectionString;

        return connectionString;
    }

    public List<CUSTOMER> GetCustomerList()
    {

        using (Entities db = new Entities())
        {
            db.Connection.ConnectionString = getConStrSQL();
            db.Connection.Open();

            var data = from p in db.CUSTOMERs
                       select p;

            db.Connection.Close();

            return data.ToList();
        }
    }

但是当我 运行 代码时,我得到以下错误:

Schema specified is not valid. Errors: error 0194: All artifacts loaded into an ItemCollection must have the same version. Multiple versions were encountered. DAL.DBModel.ssdl(2,46) : error 0172: All SSDL artifacts must target the same provider. The Provider 'Oracle.DataAccess.Client' is different from 'Oracle.DataAccess.Client' that was encountered earlier. DAL.DBModel.ssdl(2,89) : error 0169: All SSDL artifacts must target the same provider. The ProviderManifestToken '12.1' is different from '9.2' that was encountered earlier. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(3,4) : error 0019: The EntityContainer name must be unique. An EntityContainer with the name 'Schema' is already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(834,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.Table' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(844,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.TableColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(870,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.View' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(882,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ViewColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(908,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.Function' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(933,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.Procedure' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(943,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.Parameter' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(967,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.Constraint' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(979,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.CheckConstraint' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(987,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ConstraintColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(996,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ForeignKeyConstraint' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1005,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ForeignKey' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1016,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ViewConstraint' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1031,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.TableTableConstraint' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1044,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ConstraintConstraintColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1057,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ConstraintForeignKey' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1070,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.FromForeignKeyColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1083,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ToForeignKeyColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1096,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.TableTableColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1109,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ViewViewColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1122,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.FunctionFunctionParameter' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1135,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ProcedureProcedureParameter' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1148,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ViewViewConstraint' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1161,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ViewConstraintConstraintColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1174,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ViewConstraintForeignKey' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1187,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.FromForeignKeyViewColumn' was already defined. Oracle.ManagedDataAccess.src.EntityFramework.Resources.EFOracleStoreSchemaDefinition.ssdl(1200,4) : error 0019: Each type name in a schema must be unique. Type name 'Oracle.ToForeignKeyViewColumn' was already defined.

我用谷歌搜索但找不到任何答案。 请帮我。是银色的。

检查您的项目参考资料——您可能包括了一个 "brings along" 它自己的项目 database/schema。如:

  • 项目 A --> 有 EF 版本 X,数据库 project_a.edmx
  • 项目 B --> 引用 'Project A',EF 版本 Y,数据库 project_b.edmx

其中版本 X 和 Y 可能不同。

这可能是由于根据 .NET/EF 版本生成了不同的连接字符串 -- 查看更多 http://weblogs.asp.net/kencox/schema-specified-is-not-valid-error-on-upgrading-to-asp-net-4

我知道这个答案来晚了,但我希望它能为将来的参考提供参考。

我遇到了同样的问题,我发现问题在于正确指定元数据:

string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
{
    Metadata = "res://*/Models.ModelKDM.csdl|res://*/Models.ModelKDM.ssdl|res://*/Models.ModelKDM.msl",
    Provider = "Oracle.DataAccess.Client",
    ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
    {
        DataSource = server,
        UserID = user,
        Password = pass,
    }.ConnectionString
}.ConnectionString;
return connectionString;