【发布时间】: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