我在使用Entity Framework处理项目时尝试更改SQL Server数据库。然而,当我更改表并添加1个字符串列Email
时,整个系统崩溃,但出现了一个没有任何意义的异常。
我试过增加一些光线的变化,但没有效果。
例外情况:
System.Exception:“无法检索实体:无法Map属性“Users.Email”,因为它的类型为“object”,而该类型不是受支持的基元类型或有效的实体类型。请显式Map此属性,或者使用“[NotMapped]”特性或在“OnModelCreating”中使用“EntityTypeBuilder.Ignore”忽略它。
下面是DbContext
:
using System.Data.Entity;
namespace PlanetariumModelsFramework
{
public class PlanetariumServiceContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<PlanetariumServiceContext>(null);
base.OnModelCreating(modelBuilder);
}
public DbSet<Performance> Performances { get; set; }
public DbSet<Poster> Posters { get; set; }
public DbSet<Ticket> Tickets { get; set; }
public DbSet<Orders> Orders { get; set; }
public DbSet<Tier> Tiers { get; set; }
public DbSet<Hall> Halls { get; set; }
public DbSet<Users> Users { get; set; }
}
}
用户模型类:
namespace PlanetariumModelsFramework
{
public class Users
{
public int Id { get; set; }
public string Username { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string UserPassword { get; set; } = string.Empty;
public string UserRole { get; set; } = string.Empty;
}
}
发生错误的存储库类:
namespace PlanetariumRepositories
{
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected readonly PlanetariumServiceContext RepositoryPlanetarium;
public Repository(PlanetariumServiceContext repositoryPatternDemoContext)
{
RepositoryPlanetarium = repositoryPatternDemoContext;
}
public IQueryable<TEntity> GetAll()
{
try
{
return RepositoryPlanetarium.Set<TEntity>();
}
catch (Exception ex)
{
throw new Exception($"Couldn't retrieve entities: {ex.Message}");
}
}
public async Task<TEntity> AddAsync(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null");
}
try
{
await RepositoryPlanetarium.AddAsync(entity);
await RepositoryPlanetarium.SaveChangesAsync();
return entity;
}
catch (Exception ex)
{
throw new Exception($"{nameof(entity)} could not be saved: {ex.Message}");
}
}
public async Task<TEntity> UpdateAsync(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null");
}
try
{
RepositoryPlanetarium.Update(entity);
object p = await RepositoryPlanetarium.SaveChangesAsync();
return entity;
}
catch (Exception ex)
{
throw new Exception($"{nameof(entity)} could not be updated: {ex.Message}");
}
}
public async Task<TEntity> DeleteAsync(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null");
}
try
{
RepositoryPlanetarium.Remove(entity);
object p = await RepositoryPlanetarium.SaveChangesAsync();
return entity;
}
catch (Exception ex)
{
throw new Exception($"{nameof(entity)} could not be deleted: {ex.Message}");
}
}
}
}
继承它的UsersRepository
类(可选):
using PlanetariumModels;
namespace PlanetariumRepositories
{
public class UsersRepository : Repository<Users>, IUsersRepository
{
public UsersRepository(PlanetariumServiceContext repositoryPatternDemoContext) : base(repositoryPatternDemoContext)
{
}
public Task<Users> GetByUsernameAsync(string username)
{
return base.GetAll().FirstOrDefaultAsync(x => x.Username == username);
}
public Task<Users> GetByEmailAsync(string email)
{
return base.GetAll().FirstOrDefaultAsync(x => x.Email == email);
}
public new List<Users> GetAll()
{
return base.GetAll().ToList<Users>();
}
}
}
我的SQL Server表创建脚本:
我试着把类型改为System.String
,试着改变它的类型,但是没有任何效果。而且,老实说,我不知道这个错误是没有任何意义的。
1条答案
按热度按时间eoxn13cs1#
谢谢你在评论中的提示。
问题是手动更改数据库,这在使用ORM框架(如EntityFramework)时是禁止的。
简单的迁移就完全解决了这个问题。
为此,我必须在View -〉Package Manager Console中打开软件包管理器。
然后输入以下2个命令进行迁移:
Add-Migration <MigrationName>
还有...
update-database <MigrationName>