2016-11-14 84 views
3

我遵循damienbod(https://damienbod.com/2016/01/11/asp-net-5-with-postgresql-and-entity-framework-7/)的指导。我设法连接到我的PostgreSql数据库,并创建EntityMigration历史记录表,DataEventRecord表和SourceInfo表。总共有三个项目(Postgre需要两个,第三个是我的实际项目):DataAccessPostgreSqlProvider,DomainModel和iConnect。使用EF和Dot Net Core 1(PostgreSQL数据库)生成迁移时出错

我创建了一个型号,下面的代码,然后执行:添加迁移NestProtectDevices -Context NestProtectDevices

我必须指定-Context或我得到一个错误,如果我指定的-Context DomainModelPostgreSqlContext,它只是生成一个空的迁移文件(而不是从我的模型中生成一个)。这就是为什么我指定-Context作为Model的名称。以下是模型的代码和我得到的错误。让我知道是否需要其他代码来帮助调试。

using DataAccessPostgreSqlProvider; 
using DomainModel; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.EntityFrameworkCore; 
using DomainModel.Model; 

namespace iConnect.Models.Nest 
{ 
    public class NestProtectDevices : DomainModelPostgreSqlContext 
    { 
     public NestProtectDevices(DbContextOptions<DomainModelPostgreSqlContext> options) : base(options) 
     { 
     } 

     public long DeviceId { get; set; } 
     public long UserId { get; set; } 
     public int CompanyId { get; set; } 
     public long StructureId { get; set; } 
     public long WhereId { get; set; } 
     public string Name { get; set; } 
     public string NameLong { get; set; } 
     public bool IsOnline { get; set; } 
     public int BatteryHealth { get; set; } 
     public int CoAlarmState { get; set; } 
     public int SmokeAlarmState { get; set; } 
     public string UiColorState { get; set; } 
     public bool IsManualTestActive { get; set; } 
     public DateTime LastManualTestTime { get; set; } 
     public DateTime Timestamp { get; set;} 
    } 
} 

命令出现错误:

PM> add-migration NestProtectDevices -Context NestProtectDevices 
No parameterless constructor was found on 'NestProtectDevices'. Either add a parameterless constructor to 'NestProtectDevices' or add an implementation of 'IDbContextFactory<NestProtectDevices>' in the same assembly as 'NestProtectDevices'. 
+0

不是那个错误告诉你到底是什么问题? –

+0

如果我使它成为一个无参数的构造函数,那么它会出错,它需要提供选项。 – Ronny

+0

我仍然在寻找这方面的答案。 – Ronny

回答

5
using DataAccessPostgreSqlProvider; 
    using DomainModel; 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Threading.Tasks; 
    using Microsoft.EntityFrameworkCore; 
    using DomainModel.Model; 

    namespace iConnect.Models.Nest 
    { 
     public class NestProtectDevices : DomainModelPostgreSqlContext 
     { 
      public NestProtectDevices(DbContextOptions<DomainModelPostgreSqlContext> options) : base(options) 
      { 
      } 
      public DbSet<Device> Devices { get; set; } 

     } 
public class Device 
{ 
      public long DeviceId { get; set; } 
      public long UserId { get; set; } 
      public int CompanyId { get; set; } 
      public long StructureId { get; set; } 
      public long WhereId { get; set; } 
      public string Name { get; set; } 
      public string NameLong { get; set; } 
      public bool IsOnline { get; set; } 
      public int BatteryHealth { get; set; } 
      public int CoAlarmState { get; set; } 
      public int SmokeAlarmState { get; set; } 
      public string UiColorState { get; set; } 
      public bool IsManualTestActive { get; set; } 
      public DateTime LastManualTestTime { get; set; } 
      public DateTime Timestamp { get; set;} 
    } 
} 

除非本DataAccessPostgreSqlProvider比普通SQL提供那么我相信它应该更喜欢这种设置完全不同。顺便说一下,你的帖子中的链接是不好的。

+0

该链接已更新。感谢您的回应。我会试试看看它是否有效。 https://damienbod.com/2016/01/11/asp-net-5-with-postgresql-and-entity-framework-7/ – Ronny

2

你得到的错误似乎很有帮助。将一个无参数的构造函数添加到您的类IN ADDITION TO您已有的构造函数中。

public NestProtectDevices() 
{ 
} 
+0

我也得到了Sqlite和MS SQL Server数据提供者的这个错误。有效地添加无参数构造函数有助于。 –

相关问题