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

标签 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 指定为模型名称的原因。下面是模型的代码和我得到的错误。让我知道是否需要任何其他代码来帮助调试。

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 提供程序完全不同,否则我认为它应该更像这样设置。顺便说一句,您帖子中的链接不好。

关于c# - 使用 EF 和 Dot Net Core 1(PostgreSQL 数据库)生成迁移时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40599669/

相关文章:

c# - Newtonsoft.Json.JsonSerializationException 未由用户代码处理

c# - 如何在 Windows Phone 应用程序中从 StackPanel 制作位图图像

c# - 如何使用填充数组的类型对象的属性序列化类

c# - 在 .NET 中设置 HttpClient 以使其与多个线程一起工作并提供并发性

c# - 为什么在包含 System.Collections.Generic 时无法使用 SynchronizedCollection?

sql - 我如何强制 Postgres 使用特定的索引?

c# - 将 DataGridView 导出到 Excel,并应用过滤器 C#

c# - 诊断/调试潜在的堆栈损坏.NET应用程序

postgresql - 要求 PostgreSQL 中只有 1 行标记为 "default"

php - 是否有可能在插入之前获取要插入数据库的id