【问题标题】:"Unable to cast object of type 'ConcreteTypeMapping' to type 'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping" WebApi on macos“无法将'ConcreteTypeMapping'类型的对象转换为'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping”在macos上的WebApi
【发布时间】:2018-11-15 15:27:08
【问题描述】:

我已按照说明使用 Visual Studio 在 mac os 中创建 WebAPI。

https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api-mac?view=aspnetcore-2.1 https://github.com/aspnet/Docs/blob/master/aspnetcore/tutorials/first-web-api-mac.md

Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<IAdminUOW, AdminUOW>();
            services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
            services.AddDbContext<MeroRentalContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                sqlServerOption => sqlServerOption.MigrationsAssembly("Database"))
);
            services.AddMvc();
        }

MeroRentalContext.cs

数据库上下文cs文件

public class MeroRentalContext : DbContext
    {
        public MeroRentalContext(DbContextOptions<MeroRentalContext> options)
            : base(options)
        { }
        public DbSet<AdminUser> TodoItems { get; set; }
    }

AdminUser.cs 文件

public class AdminUser
    {
        public Guid UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public DateTime CreatedTimeStamp { get; set; }
        public DateTime? ModifiedTimeStamp { get; set; }
        public DateTime? LogDate { get; set; }
        public short? LogNumber { get; set; }
        public bool ReloadActiveFlag { get; set; }
        public bool isActive { get; set; }
        public string ExtraText { get; set; }
        public string ResetPasswordToken { get; set; }
        public DateTime? ResetPasswordTokenCreatedTimeStamp { get; set; }
    }

通用类到类 db 数据

public class GenericRepository<T> : IGenericRepository<T> where T : class
    {
        private readonly MeroRentalContext _entities;
        public GenericRepository(MeroRentalContext dbContext)
        {
            _entities = dbContext;
        }

        public virtual IQueryable<T> GetAll()
        {

            IQueryable<T> query = _entities.Set<T>();
            return query;
        }

        public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {

            IQueryable<T> query = _entities.Set<T>().Where(predicate);
            return query;
        }
    }

数据库中的表

CREATE TABLE dbo.AdminUser (
    UserId uniqueidentifier NOT NULL,
    FirstName varchar(max) NOT NULL,
    LastName varchar(max) NOT NULL,
    Email varchar(max) NOT NULL,
    UserName varchar(max) NOT NULL,
    Password varchar(max) NOT NULL,
    CreatedTimeStamp datetime NOT NULL DEFAULT (getdate()),
    ModifiedTimeStamp datetime NULL,
    LogDate datetime NULL,
    LogNumber smallint NULL,
    ReloadActiveFlag bit NOT NULL DEFAULT ((0)),
    isActive bit NOT NULL DEFAULT ((1)),
    ExtraText varchar(max) NULL,
    ResetPasswordToken varchar(max) NULL,
    ResetPasswordTokenCreatedTimeStamp datetime NULL
);

ALTER TABLE dbo.AdminUser ADD CONSTRAINT PK__AdminUse__1788CC4C305F59E9 PRIMARY KEY (UserId);

因为在调试时遇到错误

无法将“ConcreteTypeMapping”类型的对象转换为“Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping”类型

屏幕截图的更多详细信息

做了一些研究,但无法找到解决方案 https://github.com/aspnet/EntityFrameworkCore/issues/8369 https://github.com/aspnet/EntityFrameworkCore/issues/11704 https://www.ctolib.com/article/comments/61636

【问题讨论】:

  • IQueryable query = _entities.Set().Where(predicate);此代码产生错误
  • 是的。该代码仍然存在
  • 是的,我尝试了他们告诉的选项 - 我有最新版本的 Microsoft.EntityFrameworkCore.InMemory(2.1.0)
  • github.com/aspnet/EntityFrameworkCore/issues/… - 你几乎肯定有版本不匹配。

标签: c# asp.net asp.net-mvc entity-framework asp.net-core


【解决方案1】:

根据https://github.com/aspnet/EntityFrameworkCore/issues/11704,您的版本不匹配。

您的一些实体框架版本引用2.1,而一些引用2.0。

您需要将它们全部更改为引用版本2.1。

您应该打开所有csproj 文件并搜索2.0 和2.1。

【讨论】:

    【解决方案2】:

    这是版本不匹配的问题。只需将所有参考版本2.0 更新为2.1。

    • Microsoft.AspNetCore.All --version 2.1.0
    • Microsoft.EntityFrameworkCore --version 2.1.0
    • Microsoft.NETCore.App --version 2.1.0

    希望更改这些引用,它会起作用。

    【讨论】:

      【解决方案3】:

      我遇到了和你一样的问题。在我的例子中,我通过将其添加到包中直接将 Microsoft.EntityFramework.Design 引用到 Visual C# 项目文件 (.csproj)管理控制台:

      Install-Package Microsoft.EntityFrameworkCore.Design
      

      在我得到的描述的错误中,Microsoft.EntityFrameworkCore.Design 被抛出,我想添加它。所以它现在运行。

      【讨论】:

        【解决方案4】:

        就我而言,我只是在我的 csproj 中添加了下面的代码

            <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0-rc1-final" />
        

        我不知道这里发生了什么,因为在包管理器中找不到这个包 Microsoft.EntityFrameworkCore.SqlServer 并且它在项目依赖项文件夹中显示警告,但它的工作很神奇

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-06
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-19
          相关资源
          最近更新 更多