【发布时间】:2021-08-19 00:07:52
【问题描述】:
我有一个像这样的 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
如果我向 App、Team Device 等添加属性,我可以添加迁移并更新数据库就好了。
例子:
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 类中做同样的事情:
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
我收到以下错误:
访问 Microsoft.Extensions.Hosting 服务时出错。在没有应用程序服务提供商的情况下继续。
错误:出现一个或多个错误。 (列名“ABD”无效。)
我做错了什么?
【问题讨论】:
-
AppUser 类是什么样的?
-
@TimothyG。刚加了。我实际上能够为您在那里看到的属性添加迁移,但现在不能这样做。可能与我现在使用的 EF 版本有关。
-
您使用新属性为 AppUser 添加了迁移并确保已创建列?
-
对,但您是否创建了迁移以添加此新属性列?
标签: c# .net-core entity-framework-core entity-framework-migrations