簡體   English   中英

使用EF和Dot Net Core 1(PostgreSQL數據庫)生成遷移時出錯

[英]Error generating migration with EF and Dot Net Core 1 (PostgreSQL database)

我遵循了damienbod的教程( https://damienbod.com/2016/01/11/asp-net-5-with-postgresql-and-entity-framework-7/ )。 我設法獲得了與PostgreSql數據庫的連接,並創建了EntityMigration歷史表,DataEventRecord表和SourceInfo表。 總共有三個項目(Postgre需要兩個項目,第三個是我的實際項目):DataAccessPostgreSqlProvider,DomainModel和iConnect。

我在下面創建了一個Model,代碼,然后執行:add-migration NestProtectDevices -Context NestProtectDevices

我必須指定-Context或者我得到一個錯誤,如果我指定DomainModelPostgreSqlContext的-Context,它只會生成一個空的遷移文件(而不是從我的模型生成一個)。 這就是我將-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'.
    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提供程序完全不同,否則我認為它應該設置得更像這樣。 順便說一句,你的帖子中的鏈接是不好的。

你得到的錯誤似乎非常有幫助。 向您的類添加無參數構造函數IN ADDITION TO您已有的構造函數。

public NestProtectDevices() 
{
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM