C# 在Asp.net 5(vnext)中与Npgsql一起使用Postgres
我已将我的应用程序配置为连接到Postgres数据库,但当向其中加载一些数据时,它会给我一个错误 首先,这里是我在project.json中包含的内容:C# 在Asp.net 5(vnext)中与Npgsql一起使用Postgres,c#,asp.net-mvc,postgresql,C#,Asp.net Mvc,Postgresql,我已将我的应用程序配置为连接到Postgres数据库,但当向其中加载一些数据时,它会给我一个错误 首先,这里是我在project.json中包含的内容: "EntityFramework7.Npgsql": "3.1.0-rc1-1", 在Startup.cs类中: services.AddEntityFramework() .AddNpgsql() .AddDbContext<ApplicationDbContext>(option
"EntityFramework7.Npgsql": "3.1.0-rc1-1",
在Startup.cs类中:
services.AddEntityFramework()
.AddNpgsql()
.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]));
用于更新数据库的dnx命令运行得很好,但我必须手动创建“\uuuEFmigrationsHistory”表
执行context.SaveChanges
时出现以下错误:
{Microsoft.Data.Entity.DbUpdateException:更新条目时出错。有关详细信息,请参阅内部异常。-->Npgsql.NpgsqlException:23502:列“Id”中的null值违反了not null约束
em Npgsql.NpgsqlConnector.DoReadSingleMessage(DataRowLoadingMode DataRowLoadingMode,布尔返回NullForAsyncMessage,布尔isPrependedMessage)
em Npgsql.NpgsqlConnector.ReadSingleMessageWithPrepend(DataRowLoadingMode DataRowLoadingMode,布尔返回NullForAsyncMessage)
em Npgsql.NpgsqlConnector.ReadSingleMessage(DataRowLoadingMode DataRowLoadingMode)
em Npgsql.NpgsqlDataReader.ReadMessage()
em Npgsql.NpgsqlDataReader.Init()
em Npgsql.NpgsqlCommand.Execute(CommandBehavior)
em Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior)
em Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior)
em System.Data.Common.DbCommand.ExecuteReader()
em Microsoft.Data.Entity.Storage.Internal.RelationalCommand.c__DisplayClass17_0.b__0(DbCommand cmd,IRelationalConnection con)
em Microsoft.Data.Entity.Storage.Internal.RelationalCommand.Execute[T](IRelationalConnection连接,Func3操作,字符串executeMethod,布尔openConnection,布尔closeConnection)
em Microsoft.Data.Entity.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection连接,布尔管理连接)
em Microsoft.Data.Entity.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection)
---国际例外情况处理职能指令手册---
em Microsoft.Data.Entity.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection)
em Microsoft.Data.Entity.Update.Internal.BatchExecutor.Execute(IEnumerable
1 commandBatches,IRelationalConnection)
em Microsoft.Data.Entity.Storage.RelationalDatabase.SaveChanges(IReadOnlyList1条目)
em Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList
1 entriesToSave)
em Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(布尔接受更改成功)
em Microsoft.Data.Entity.DbContext.SaveChanges(布尔接受更改成功)
em Microsoft.Data.Entity.DbContext.SaveChanges()
em SQLite.Migrations.Seed.CriaEndereco()na F:\Dados\Documents\visualstudio 2015\Projects\SQLite\src\SQLite\Migrations\Seed.cs:linha 73
em SQLite.Migrations.Seed.Execute(IServiceProvider应用程序服务)na F:\Dados\Documents\Visual Studio 2015\Projects\SQLite\src\SQLite\Migrations\Seed.cs:linha 36
em SQLite.Startup.CreateSampleData(IServiceProvider应用程序服务)na F:\Dados\Documents\Visual Studio 2015\Projects\SQLite\src\SQLite\Startup.cs:linha 138
em SQLite.Startup.Configure(IAApplicationBuilder应用程序、IHostingEnvironment环境、ILoggerFactory loggerFactory)na F:\Dados\Documents\Visual Studio 2015\Projects\SQLite\src\SQLite\Startup.cs:linha 128}
Id列是否应按实体分配
同样的代码也适用于SqlServer
更新
以下是SqlServer表架构:
CREATE TABLE [dbo].[Ufs] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Nome] NVARCHAR (70) NOT NULL,
[Sigla] NVARCHAR (2) NOT NULL,
CONSTRAINT [PK_Uf] PRIMARY KEY CLUSTERED ([Id] ASC)
);
这里是博士后:
CREATE TABLE public."Ufs"
(
"Id" integer NOT NULL,
"Nome" text NOT NULL,
"Sigla" text NOT NULL,
CONSTRAINT "PK_Uf" PRIMARY KEY ("Id")
)
WITH (
OIDS=FALSE
);
ALTER TABLE public."Ufs"
OWNER TO postgres;
所有这些代码都是通过迁移自动生成的。Postgres数据库中的“Id”列应该是“串行”类型或类似的类型
Postgres“Integer”类型不是自动递增类型。
默认情况下,分配ID是数据库的角色
尝试在“Id”属性上方添加此批注
DatabaseGenerated(DatabaseGeneratedOption.Identity)
雷尔。
看起来您使用SqlServer迁移生成了Postgres数据库
我建议您恢复并删除所有迁移,然后再次添加迁移。但我认为有明确的信息说明问题的原因:Npgsql.NpgsqlException:23502:Id列中的null值违反了非null约束em Npgsql.NpgsqlConnector.DoReadSingleMessage。您正在为不能有空值的列提供空值。你能提供Postgre表的模式吗?此表的SqlServer版本可能具有列的标识设置。
DatabaseGenerated(DatabaseGeneratedOption.Identity)