Ronny
Ronny

Reputation: 157

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

I followed a tutorial by damienbod ( https://damienbod.com/2016/01/11/asp-net-5-with-postgresql-and-entity-framework-7/ ). I managed to get a connection to my PostgreSql database, and it creates the EntityMigration history table, the DataEventRecord table and the SourceInfo table. There are a total of three projects (two are needed for Postgre, and the third is my actual project): DataAccessPostgreSqlProvider, DomainModel, and iConnect.

I've created a Model, code below, and then execute: add-migration NestProtectDevices -Context NestProtectDevices

I have to specify the -Context or I get an error, and if I specify the -Context of DomainModelPostgreSqlContext, it just generates an empty migration file (as opposed to generating one from my model). That is why I specified the -Context as the Model name. Below are the code for the Model and the error I am getting. let me know if any other code would be needed to help in debugging.

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

Command with error:

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'.

Upvotes: 3

Views: 715

Answers (2)

ScottTx
ScottTx

Reputation: 1483

The error you're getting seems pretty helpful. Add a parameterless constructor to your class IN ADDITION TO the constructor you already have.

public NestProtectDevices() 
{
}

Upvotes: 2

Patrick Mcvay
Patrick Mcvay

Reputation: 2281

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

Unless this DataAccessPostgreSqlProvider is completely different than the regular Sql provider then I believe it should be set up more like this. By the way that link in your post is no good.

Upvotes: 6

Related Questions