【问题标题】:Invalid column name in EF CoreEF Core 中的列名无效
【发布时间】: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


【解决方案1】:

好的,所以我发现了问题。

我的Startup.cs 中有一个SeedUsers 方法,因为add-migration 首先运行整个项目,所以由于未迁移的数据库和我的新代码不匹配,它遇到了异常。

我刚刚注释掉了对SeedUsers 的调用,它起作用了。

感谢您的帮助。

【讨论】:

  • 啊,很好,我什至没有想到这一点。我想知道为什么有些东西试图利用您想通过迁移添加的列,这绝对是有道理的。
猜你喜欢
  • 1970-01-01
  • 2022-12-20
  • 2021-04-29
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2014-02-14
相关资源
最近更新 更多