且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架种子方法异常

更新时间:2023-02-25 18:09:11

将Oracle.ManagedDataAccess.dll安装到GAC解决了我的问题。如果您的机器上安装了Oracle Client或Oracle数据库,可能是原因。

  C:\Windows\system32> cd E:\smn\packages\Oracle.ManagedDataAccess.12.1.021\lib\\\
et40

C:\Windows\system32> e:

E:\smn\packages\Oracle.ManagedDataAccess.12.1.021\lib\\\
et40>C:\Program Files(x86)\Microsoft SDKs\Windows\v8.1A\bin \NETFX 4.5.1 Tools\gacutil.exe/ i Oracle.ManagedDataAccess.dll
Microsoft(R).NET全局程序集缓存实用程序。版本4.0.30319.33440
版权所有(c)微软公司。版权所有。

装配成功添加到缓存


I am using Entity Framework 6 with an MVC5 webapi2 project against an Oracle database.

I am trying to add some ApplicationRoles in the Seed method, but when I execute update-database I get this exception:

Running Seed method. System.Runtime.Serialization.SerializationException: Type is not resolved for member 'Oracle.ManagedDataAccess.Client.OracleException,Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342'. at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) Type is not resolved for member 'Oracle.ManagedDataAccess.Client.OracleException,Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342'.

My code in the Seed method:

var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);

if (!context.Roles.Any(r => r.Name == "USER"))
{
    var role = new IdentityRole("USER");
    roleManager.Create(role);
}

update-database is creating my tables without any problem, it's just the Seed method which has a problem.

Does anyone have any suggestions as to what the problem could be please?

Thanks.

Installing Oracle.ManagedDataAccess.dll to GAC resolved my issue. If you installed Oracle Client or Oracle Database on your machine that might be the cause.

C:\Windows\system32>cd E:\smn\packages\Oracle.ManagedDataAccess.12.1.021\lib\net40

C:\Windows\system32>e:

E:\smn\packages\Oracle.ManagedDataAccess.12.1.021\lib\net40>"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\gacutil.exe" /i Oracle.ManagedDataAccess.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.33440
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache