2016-11-14 41 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數據提供者的這個錯誤。有效地添加無參數構造函數有助於。 –