[英]Using Postgres in Asp.net 5 (vnext) with Npgsql
我已经将我的应用程序配置为连接到Postgres数据库,但是在向其中加载一些数据时,它给了我一个错误。
首先,这是我包含在project.json中的内容:
"EntityFramework7.Npgsql": "3.1.0-rc1-1",
并在Startup.cs类中:
services.AddEntityFramework()
.AddNpgsql()
.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]));
这是我的连接字符串:
Server=127.0.0.1;Port=5432;Database=test1;User Id=postgres;Password=mypass123
dnx命令更新数据库效果很好,但是我必须手动创建“ __EFMigrationsHistory”表。
问题是执行context.SaveChanges
时出现以下错误:
{Microsoft.Data.Entity.DbUpdateException:更新条目时发生错误。 有关详细信息,请参见内部异常。 ---> Npgsql.NpgsqlException:23502:“ Id”列中的空值违反了非空约束em )em Npgsql.NpgsqlConnector.ReadSingleMessage(DataRowLoadingMode dataRowLoadingMode)em Npgsql.NpgsqlDataReader.ReadMessage()em Npgsql.NpgsqlDataReader.Init()em Npgsql.NpgsqlCommand.Execute(CommandBehavior行为)em Npgsql.NpgsqlCommand.Indbternal() 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](IRelationalConnect 离子连接,功能
3 action, String executeMethod, Boolean openConnection, Boolean closeConnection) em Microsoft.Data.Entity.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, Boolean manageConnection) em Microsoft.Data.Entity.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection) --- Fim do rastreamento de pilha de exceções internas --- em Microsoft.Data.Entity.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection) em Microsoft.Data.Entity.Update.Internal.BatchExecutor.Execute(IEnumerable
1 commandBatches,IRelationalConnection连接),Microsoft.Data.Entity.Storage.RelationalDatabase.SaveChanges(IReadOnlyList1 entries) em Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList
Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList1 entries) em Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList
保存)和Microsoft.Data.Entity.ChangeTracking。 Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)em Microsoft.Data.Entity.DbContext.SaveChanges(Boolean acceptAll ChangesOnSuccess)em Microsoft.Data.Entity.DbContext.SaveChanges()em SQLite.Migrations.Seed.CriaEndereco()na F:\\ Dados \\ Documents \\ Visual Studio 2015 \\ Projects \\ SQLite \\ src \\ SQLite \\ Migrations \\ Seed.cs: linha 73 em SQLite.Migrations.Seed.Execute(IServiceProvider applicationServices)不适用F:\\ Dados \\ Documents \\ Visual Studio 2015 \\ Projects \\ SQLite \\ src \\ SQLite \\ Migrations \\ Seed.cs:linha 36 em SQLite.Startup.CreateSampleData(IServiceProvider F:\\ Dados \\ Documents \\ Visual Studio 2015 \\ Projects \\ SQLite \\ src \\ SQLite \\ Startup.cs:linha 138 em SQLite.Startup.Configure(IApplicationBuilder应用程序,IHostingEnvironment env,ILoggerFactory loggerFactory)无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)
);
在这里Postgres:
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;
所有这些代码都是通过迁移自动生成的。
看起来您是使用SqlServer迁移生成了Postgres数据库的。
我建议您还原并删除所有迁移,然后再次添加迁移。
Postgres数据库中的“ Id”列应为“ Serial”类型或类似名称。
Postgres的“整数”类型不是自动递增的类型。 默认情况下,分配ID是数据库的角色。
尝试将此注释添加到“ Id”属性上方。
DatabaseGenerated(DatabaseGeneratedOption.Identity)
相对。 这是一个相关的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.