账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    EF6 Oracle code first 报错
    59
    0

    按照教程
    https://www.cnblogs.com/yjmyz...

    “System.IO.FileLoadException”类型的未经处理的异常在 mscorlib.dll 中发生

    其他信息: 未能加载文件或程序集“Oracle.ManagedDataAccess.EntityFramework,
    Version=6.122.1.0, Culture=neutral,
    PublicKeyToken=89b483f429c47342”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自
    HRESULT:0x80131040)

    可以读数据但是能不能创建表。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 心痛的网名繁体字 普通会员 1楼

      在EF6 (Entity Framework 6) 的 Code First 调试和错误排查中,以下是一些可能的解决方案和错误代码:

      1. Entity Framework Version Compatibility Problem: 如果EF6使用了不兼容的Entity Framework版本,如7.0.1或更高版本,可能会引发Code First错误。确保你的EF6安装在正确的版本,可以使用以下命令更新: Update-Package Entity Framework -Version 7.0.1

      2. Missing dependencies or missing assemblies: 如果你的项目中包含的EF6依赖项或依赖项的 assemblies 包装不完整,可能会导致代码 First 出现错误。确保你的项目包含以下依赖项: csharp <dependentAssembly> <assemblyIdentity name="Entity Framework" publicKeyToken="6598530050101e49" culture="neutral"/> <bindingRedirect location="YourEF6Location/YourEF6Version.dll" /> </dependentAssembly>

      请将YourEF6Location 替换为你的EF6安装目录,将YourEF6Version.dll 替换为你的EF6安装的版本文件(例如,YourEF6Version.dllEF6.SqlClient.dll)。

      1. ASP.NET Core Entity Framework Configuration Error: If you are using ASP.NET Core with EF6 and you have a configuration error, it could cause the Code First pipeline to fail. Make sure you have the correct EF Core configuration set up for your project, and it includes the necessary Entity Framework assemblies and settings. csharp <configuration> <appsettings> <add key="EntityFramework.DefaultConnectionFactory" value="System.Data.SqlClient.SqlConnectionFactory" /> <add key="EntityFramework.UseSqlServer" value="true" /> <add key="EntityFramework.ConnectionStrings" value="your_connection_string" /> </appsettings> </configuration>

      If your project is using Entity Framework Core v3, you will need to replace System.Data.SqlClient.SqlConnectionFactory with System.Data.Entity.SqlServer.SqlConnectionFactory and your_connection_string with your EF Core connection string.

      1. Incompatible Database Provider: If your project is using an incompatible database provider like SQLite or SQL Server, the EF6 Code First pipeline may fail due to database-specific features or constraints. Make sure your project is using a compatible database provider, such as SQLite or SQL Server. <entityFramework> <defaultConnectionFactory type="System.Data.SqlClient.SqlConnectionFactory" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.SqlClient.SqlProvider" /> </providers> </entityFramework>

      If you are using Entity Framework Core v2, you will need to specify the providerInvariantName parameter to specify the database provider to use. 5. Database Model Changes: If your project is using an updated database model that requires a specific EF6 change, such as changes to entity properties, associations, or relationships, the Code First pipeline may fail due to incompatibility with the existing EF6 model. To handle this, you can create a migration script that updates the database model to match the new version of EF6, or you can manually modify the database schema in your application. csharp Create-Database migrator -Target Database=my_new_db -DatabaseModelPath my_new_db/models.cs -OutputMigrationFile migration.up.mdf

      After creating the migration file, run the migration script using the following command: .\migrations\up.mdf

      1. Use of Unrecognized Entity Framework Database Model Version: If your database model is using an older version of Entity Framework, the Code First pipeline may fail due to the lack of support for the new EF6 version. To resolve this, you can specify the appropriate database model version in your EF6 configuration or in the database model itself. For example, in an ASP.NET Core application, you can use the Models section of your Startup.cs file to specify the database model version: csharp public void ConfigureServices(IServiceCollection services) { // ... your other configuration and services ... services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( "your_connection_string", // specify the EF Core database model version new SqlClientSettings() .DatabaseModelVersion = "7.0.1" .EntityFrameworkVersion = "6.0.0" // or your EF Core version .DefaultConnectionFactory = "System.Data.SqlClient.SqlConnectionFactory" .DefaultConnectionString = "your_connection_string" // or your EF Core connection string )); }

      Alternatively, you can include the necessary EntityModel and DbContext classes in your database model, and specify the database model version in the AddDbContext method: ```csharp public class ApplicationDbContext : DbContext { public DbSet Products { get; set; } public DbSet Orders { get; set; } public DbSet OrderItems { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }

      public class Order { public int Id { get; set; } public string CustomerName { get; set; } public decimal TotalPrice { get; set; } }

      public class OrderItem { public int Id { get; set; } public decimal Quantity { get; set; } public decimal PricePerUnit { get; set; } } ```

      Ensure that you replace your_connection_string and your_entityframework_version with your actual database connection string and the version of Entity Framework you are using.

      1. No DbContext configured: If your project is missing a DbContext, the Code First pipeline will fail due to a missing DbContext class or property. Ensure that you have a DbContext class that inherits from DbContext and implements the OnModelCreating method to configure your EF6 model. ```csharp public class MyDbContext : DbContext { public DbSet Products { get; set; } public DbSet Orders { get; set; } public DbSet OrderItems { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasKey(p => p.Id); modelBuilder.Entity() .HasKey(o => o.Id); modelBuilder.Entity() .HasKey(i => i.Id); modelBuilder.Entity() .Property(p => p.Name).HasConversion( typeof(String).ToMemberName(), typeof(String).ToDatabaseColumn() ); modelBuilder.Entity() .Property(o => o.CustomerName).HasConversion( typeof(String).ToMemberName(), typeof(String).ToDatabaseColumn() ); modelBuilder.Entity() .Property(i => i.Quantity).HasConversion( typeof(Int32).ToMemberName(), typeof(Int32).ToDatabaseColumn() ); modelBuilder.Entity() .Property(i => i.PricePerUnit).HasConversion( typeof(decimal).ToMemberName(), typeof(decimal).ToDatabaseColumn() ); } } ```

      This example defines a MyDbContext class with OnModelCreating method that configures the EF6 model using custom conversions for entity properties. Make sure you replace my_new_db with your actual database name and my_new_db/models.cs with the path to your database model file.

      1. Custom Model Mapping Not Defined: If you have defined custom model mappings in your DbContext class, but they are not being mapped in the Code First pipeline, the pipeline may fail due to missing or invalid mapping. To fix this, make sure you have added the OnModelCreating method to your DbContext class and configured the OnModelCreating method to map your custom model classes to the database table(s) using the ToModelMap method. ```csharp public class MyDbContext : DbContext { public DbSet Products { get; set; } public DbSet Orders { get; set; } public DbSet OrderItems { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasKey(p => p.Id); modelBuilder.Entity() .HasKey(o => o.Id); modelBuilder.Entity() .HasKey(i => i.Id); modelBuilder.Entity() .Property(p => p.Name).HasConversion( typeof(String).ToMemberName(), typeof(String).ToDatabaseColumn() ); modelBuilder.Entity() .Property(o => o.CustomerName).HasConversion( typeof(String).ToMemberName(), typeof(String).ToDatabaseColumn() ); modelBuilder.Entity() .Property(i => i.Quantity).HasConversion( typeof(Int32).ToMemberName(), typeof(Int32).ToDatabaseColumn() ); modelBuilder.Entity() .Property(i => i.PricePerUnit).HasConversion( typeof(decimal).ToMemberName(), typeof(decimal).ToDatabaseColumn() ); modelBuilder.Entity() .

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部