EF Core 中的列名无效
Invalid column name in EF Core
我有一个像这样的 IdentityDbContext:
public class MyIdentityContext : IdentityDbContext<AppUser, AppRole, int>
{
public DbSet<App> Apps { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<Avatar> Avatars { get; set; }
public DbSet<Device> Devices { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
public MyIdentityContext(DbContextOptions<MyIdentityContext> options) : base(options)
{
} // ctor
} // Cls
如果我将 属性 添加到应用程序、团队设备等,我可以添加迁移并更新数据库。
示例:
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<AppUser> Members { get; set; }
public string ABD { get; set; } <--NEW PROPERTY
} // Cls
但是如果我在我的 AppUser 中做同样的事情 class:
public class AppUser : IdentityUser<int>
{
public string FirstName { get; set; }
public string LastName { get; set; }
/// <summary>
/// What app is this user connected to.
/// </summary>
public string ApplicationName { get; set; }
/// <summary>
/// Where does the employee work
/// </summary>
public string Company { get; set; }
/// <summary>
/// What section of the company does the employee work in.
/// </summary>
public string Department { get; set; }
/// <summary>
/// Maximum device this user can use - 0 means unlimited
/// </summary>
public int DeviceLimit { get; set; } = 0;
/// <summary>
/// This users avatar
/// </summary>
public Avatar Avatar { get; set; }
/// <summary>
/// Devices connected to this user
/// </summary>
public ICollection<Device> Devices { get; set; }
/// <summary>
/// Teams that this user is a member of
/// </summary>
public ICollection<Team> Teams { get; set; }
/// <summary>
/// Apps that this user is subscribed to
/// </summary>
public ICollection<Subscription> Subscriptions { get; set; }
public string ABD { get; set; } <--NEW PROPERTY (causes error)
public AppUser()
{ }
} // Cls
我收到以下错误:
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider.
Error: One or more errors occurred. (Invalid column name 'ABD'.)
我做错了什么?
好的,所以我想出了问题。
我的 Startup.cs
中有一个 SeedUsers
方法,因为 add-migration
首先运行整个项目,它会遇到异常,因为未迁移的数据库和我的不匹配新代码。
我刚刚注释掉了对 SeedUsers
的调用,它起作用了。
感谢您的帮助。
我有一个像这样的 IdentityDbContext:
public class MyIdentityContext : IdentityDbContext<AppUser, AppRole, int>
{
public DbSet<App> Apps { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<Avatar> Avatars { get; set; }
public DbSet<Device> Devices { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
public MyIdentityContext(DbContextOptions<MyIdentityContext> options) : base(options)
{
} // ctor
} // Cls
如果我将 属性 添加到应用程序、团队设备等,我可以添加迁移并更新数据库。
示例:
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<AppUser> Members { get; set; }
public string ABD { get; set; } <--NEW PROPERTY
} // Cls
但是如果我在我的 AppUser 中做同样的事情 class:
public class AppUser : IdentityUser<int>
{
public string FirstName { get; set; }
public string LastName { get; set; }
/// <summary>
/// What app is this user connected to.
/// </summary>
public string ApplicationName { get; set; }
/// <summary>
/// Where does the employee work
/// </summary>
public string Company { get; set; }
/// <summary>
/// What section of the company does the employee work in.
/// </summary>
public string Department { get; set; }
/// <summary>
/// Maximum device this user can use - 0 means unlimited
/// </summary>
public int DeviceLimit { get; set; } = 0;
/// <summary>
/// This users avatar
/// </summary>
public Avatar Avatar { get; set; }
/// <summary>
/// Devices connected to this user
/// </summary>
public ICollection<Device> Devices { get; set; }
/// <summary>
/// Teams that this user is a member of
/// </summary>
public ICollection<Team> Teams { get; set; }
/// <summary>
/// Apps that this user is subscribed to
/// </summary>
public ICollection<Subscription> Subscriptions { get; set; }
public string ABD { get; set; } <--NEW PROPERTY (causes error)
public AppUser()
{ }
} // Cls
我收到以下错误:
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider.
Error: One or more errors occurred. (Invalid column name 'ABD'.)
我做错了什么?
好的,所以我想出了问题。
我的 Startup.cs
中有一个 SeedUsers
方法,因为 add-migration
首先运行整个项目,它会遇到异常,因为未迁移的数据库和我的不匹配新代码。
我刚刚注释掉了对 SeedUsers
的调用,它起作用了。
感谢您的帮助。