San*_*isy 8 c# asp.net asp.net-mvc entity-framework asp.net-core
我已按照指令使用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();
}
Run Code Online (Sandbox Code Playgroud)
MeroRentalContext.cs
数据库上下文cs文件
public class MeroRentalContext : DbContext
{
public MeroRentalContext(DbContextOptions<MeroRentalContext> options)
: base(options)
{ }
public DbSet<AdminUser> TodoItems { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
类数据库的通用类
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;
}
}
Run Code Online (Sandbox Code Playgroud)
数据库中的表
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);
Run Code Online (Sandbox Code Playgroud)
因为在调试时遇到错误
无法将类型为'ConcreteTypeMapping'的对象强制转换为'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping
完成了一些研究,但无法找到解决方案 https://github.com/aspnet/EntityFrameworkCore/issues/8369 https://github.com/aspnet/EntityFrameworkCore/issues/11704 https://www.ctolib.com/文章/评论/ 61636
mjw*_*lls 12
根据https://github.com/aspnet/EntityFrameworkCore/issues/11704,您的版本不匹配.
某些Entity Framework版本正在引用,2.1而有些正在引用2.0.
您需要将它们全部更改为引用版本2.1.
您应该打开所有csproj文件并搜索2.0和2.1.
| 归档时间: |
|
| 查看次数: |
7182 次 |
| 最近记录: |