首页 > 解决方案 > “无法将'ConcreteTypeMapping'类型的对象转换为在macos上键入'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping”WebApi

问题描述

我已按照说明使用 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

启动.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/文章/评论/61636

标签: c#asp.netasp.net-mvcentity-frameworkasp.net-core

解决方案


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

您的一些实体框架版本正在引用2.1,而另一些正在引用2.0.

您需要将它们全部更改为参考版本2.1

您应该打开所有csproj文件并搜索2.02.1


推荐阅读