Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unable to cast object of type 'ConcreteTypeMapping' to type 'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping" WebApi on macos

I, have followed the instruction to create the WebAPI in mac os using visual studio.

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

Database context cs file

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

AdminUser.cs file

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; }
    }

Generic class to class db data

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;
        }
    }

Table in database

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);

Since while debugging getting an errors as

Unable to cast object of type 'ConcreteTypeMapping' to type 'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping

More Details on screen shot details of error

Done some research but not able to find the solution https://github.com/aspnet/EntityFrameworkCore/issues/8369 https://github.com/aspnet/EntityFrameworkCore/issues/11704 https://www.ctolib.com/article/comments/61636

like image 644
San Jaisy Avatar asked Jun 06 '18 03:06

San Jaisy


4 Answers

As per https://github.com/aspnet/EntityFrameworkCore/issues/11704 you have a version mismatch.

Some of your Entity Framework versions are referencing 2.1 while some are referencing 2.0.

You need to change them all to reference version 2.1.

You should open up all of the csproj files and search for 2.0 and 2.1.

like image 158
mjwills Avatar answered Sep 28 '22 03:09

mjwills


It's a version mismatch problem. Just update all reference version 2.0 to 2.1.

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

Hopefully changing these references, it will work.

like image 22
Riyad Khan Avatar answered Sep 28 '22 03:09

Riyad Khan


I got exactly the same issue like you. In my case, I referenced directly the Microsoft.EntityFramework.Design to the Visual C# project file (.csproj) by adding this to the Package Manager Console :

Install-Package Microsoft.EntityFrameworkCore.Design

In the described error i got, the Microsoft.EntityFrameworkCore.Design is thrown and I thought to add it. So it runs now.

like image 41
Dasikely Avatar answered Sep 28 '22 05:09

Dasikely


In my case, i just added the code bellow in my csproj

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

I don´t know wtf happened here, because this package Microsoft.EntityFrameworkCore.SqlServer cannot be found in Package Manager and it shows warning in project dependencies folder, BUT IT WORKED MAGICALLY

like image 37
Gabriel Guilhem Avatar answered Sep 28 '22 03:09

Gabriel Guilhem