使用EF和Dot Net Core 1(PostgreSQL数据库)生成迁移时出错

时间:2016-11-14 23:36:47

标签: c# .net postgresql asp.net-core asp.net-core-1.0

我遵循了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'.

2 个答案:

答案 0 :(得分: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提供程序完全不同,否则我认为它应该设置得更像这样。顺便说一下你帖子中的链接不好。

答案 1 :(得分:2)

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

public NestProtectDevices() 
{
}