.Net SQL 数据库无法识别模型变量

.Net SQL database does not recognize model variables

我正在处理一个 .Net API 项目。问题是,我的电脑有点慢,而且我正在使用 VS 2013。一段时间后,我安装了 VS 2015,在我删除 VS 2013 之前它一直运行良好。此外,VS 2013 的卸载非常困难,因为我必须遵循此指南: Cannot uninstall, install or repair Visual Studio 2012 & 2013(下载整个安装程序,并使用命令行)。现在的问题是 SQL 服务器不接受我传递的 ManagerIds 和 WorkerIds。 我的文件 class:

 public class ConstructionSite
 {
    public int Id { get; set; }
    public int CreatorId { get; set; }
    public List<int> ManagerIds { get; set; }
    public List<int> WorkerIds { get; set; }
    public DateTime CreatedAt { get; set; }
    public bool IsActive { get; set; }
 }

但是当我点击 "Show table data" 按钮时,它只给我这种视图: a busy cat

我试着改变 Configuration.cs class 像这样的种子方法:

protected override void Seed(ReportRest.Models.ApplicationDbContext context)
{
    context.ConstructionSites.AddOrUpdate(new ConstructionSite
    {
        CreatorId = 2,
        CreatedAt = new DateTime(2000, 01, 01),
        IsActive = false,
        ManagerIds = new List<int> {1},
        WorkerIds = new List<int> {2},
    });
}

然后我删除了现有的 Migrations 文件夹,打开了 Nugget 包管理器控制台并触发了这些命令: Enable-Migrations Add-Migration 第一 Update-Database。 不幸的是,这并没有帮助我恢复 ManagerIds 和 WorkerIds。因此,在处理不稳定的 Microsoft 设置之前,我想问您是否有办法提醒我的数据库忘记变量。提前致谢!

不太确定您缺少哪些列,因为所有 4 个正常列都显示在数据库中,并且两个列表通常应该存储在自己的数据库中-table。

但是如果您使用 ASP.net 和 EntityFramework 的默认设置并且您已经有一个迁移文件夹,那么应该还有一个“__MigrationHistory”-Table 在数据库中。

意味着,如果您删除了旧的 Migrations 文件夹,您还需要删除 table 的条目。之后你可以创建一个新的 "Add-Migration InitialCreate" 并且应该得到你当前代码库的完整第一次迁移。

// 创建新的初始数据库更新以获得未来更改的工作迁移:

  1. 添加新的迁移,
  2. 然后从新的迁移文件中删除 Up() 和 Down() Methode 的内容
  3. 运行 更新数据库(为 __MigrationHistory-table 创建条目)
  4. migration-file中的strg-Z备份原内容
  5. 之后您可以运行添加迁移和更新数据库以供将来更改模型

如果你想与经理和工人建立一对一的关系,你必须创建这样的东西:

public class MyDbContext : DbContext
{
    public DbSet<ConstructionSite> ConstructionSites { get; set; }
    public DbSet<Manager> Managers { get; set; }
    public DbSet<Worker> Workers { get; set; }

    public MyDbContext()
        : base("YourConnectionStringName")
    {

    }
}

public class ConstructionSite
{
    public Guid Id { get; set; }
    public int CreatorId { get; set; }
    public List<Manager> Managers { get; set; }
    public List<Worker> Workers { get; set; }
    public DateTime CreatedAt { get; set; }
    public bool IsActive { get; set; }
}

public class Manager
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Guid ConstructionSiteId { get; set; }
}

public class Worker
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Guid ConstructionSiteId { get; set; }
}

这会创建你想要的关系