简体   繁体   English

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

[英].Net SQL database does not recognize model variables

I am working with a .Net API project. 我正在使用.Net API项目。 The trouble is, that I have a kind of slow computer and I was working with VS 2013 . 问题是我的计算机运行缓慢,并且正在使用VS 2013 After some time I installed VS 2015 , and it worked perfectly until I removed VS 2013 . 一段时间后,我安装了VS 2015 ,并且在我删除VS 2013之前,它运行良好。 Besides, the uninstallation of VS 2013 was pretty difficult, because I had to follow up this guide: Cannot uninstall, install or repair Visual Studio 2012 & 2013 (download whole setup, and use command line). 此外, VS 2013的卸载非常困难,因为我必须遵循本指南: 无法卸载,安装或修复Visual Studio 2012&2013 (下载整个安装程序,并使用命令行)。 Now problem is that now SQL server does not accept ManagerIds and WorkerIds that I pass. 现在的问题是,现在的SQL Server不接受我传递的ManagerId和WorkerId。 My document 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; }
 }

But when I hit "Show table data" button, it only gives me this type of view: 但是,当我点击“显示表数据”按钮时,它只给我这种类型的视图: 忙碌的猫

I tried to change Configuration.cs class Seed method like this: 我试图像这样更改Configuration.cs类的Seed方法:

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},
    });
}

Then I removed existing Migrations folder, opened Nugget Package Manager Console and fired these commands: Enable-Migrations Add-Migration First Update-Database. 然后,我删除了现有的Migrations文件夹,打开了Nugget软件包管理器控制台并触发了以下命令:Enable-Migrations Add-Migration First Update-Database。 Unfortunately, that hadn't helped me to recover ManagerIds and WorkerIds. 不幸的是,那并没有帮助我恢复ManagerId和WorkerId。 So, before messing with unstable Microsoft setups, I would like to ask you if there is a way to remind my database of forgotten variables. 因此,在处理不稳定的Microsoft安装程序之前,我想问您是否有一种方法可以使我的数据库中忘记变量。 Thanks in advance! 提前致谢!

Not realy sure what columns your are missing, since all 4 normal columns are shown in database and the both lists should normaly be stored in own database-tables. 不太确定您缺少哪些列,因为所有4个普通列都显示在数据库中,并且两个列表通常都应存储在自己的数据库表中。

But if you use the default setup with ASP.net and EntityFramework and you already had a Migrations-folder, there should be also a "__MigrationHistory"-Table in database. 但是,如果您将默认设置与ASP.net和EntityFramework一起使用,并且已经有一个Migrations文件夹,则数据库中还应该有一个“ __MigrationHistory”表。

Means, if you deleted the old Migrationsfolder, your are also needed to delete the entries of the table. 意味着,如果您删除了旧的Migrationsfolder,则还需要删除表中的条目。 After that you can create an new "Add-Migration InitialCreate" and should get a whole first Migration of your current code base. 之后,您可以创建一个新的“ Add-Migration InitialCreate”,并且应该对当前代码库进行完整的第一次迁移。

// For creating the new initial Datbase-Update to get a working migration for future changes: //为了创建新的初始Datbase-Update以获取将来的更改的有效迁移:

  1. Add the new Migration, 添加新的迁移,
  2. then cut the content of Up() and Down() Methode from new the migration file 然后从新的迁移文件中剪切Up()和Down()方法的内容
  3. Run Update-Database (to create the entry for __MigrationHistory-table) 运行更新数据库(以创建__MigrationHistory-table的条目)
  4. Strg-Z in the migration-file to backup the original content 迁移文件中的Strg-Z备份原始内容
  5. After that you are able to run Add-Migration and Update-Database for future changes of your Models 之后,您可以运行“添加迁移”和“更新数据库”以用于模型的将来更改

If you want one-to-may relations to Managers and Workers, you have to create something like this: 如果要与经理和工人建立一对一关系,则必须创建如下所示的内容:

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; }
}

This creates relations you want 这会创建您想要的关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM