我已经配置了我的应用程序以连接到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 Npgsql.NpgsqlConnector.DoReadSingleMessage(DataRowLoadingMode dataRowLoadingMode,Boolean returnNullForAsyncMessage,Boolean isPrependedMessage)em Npgsql.NpgsqlConnector.ReadSingleMessageowPowder )em Npgsql.NpgsqlConnector.ReadSingleMessage(DataRowLoadingMode dataRowLoadingMode)em Npgsql.NpgsqlDataReader.ReadMessage()em Npgsql.NpgsqlDataReader.Init()em Npgsql.NpgsqlCommand.Execute(CommandBehavior行为)em Npgsql.NpgsqlCommand.Indbternal() NpgsqlCommand。
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个命令批处理,IRelationalConnection连接)em Microsoft.Data.Entity.Storage.RelationalDatabase.SaveChanges(IReadOnlyList1 entries) em Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList
1个条目(保存)em Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)em Microsoft.Data.Entity.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)em Microsoft.Data.Entity.DbContext.SaveChanges()em SQLite。 Migrations.Seed.CriaEndereco()无F:\ Dados \ Documents \ Visual Studio 2015 \ Projects \ SQLite \ src \ SQLite \ Migrations \ Seed.cs:linha 73 em SQLite.Migrations.Seed.Execute(IServiceProvider applicationServices)无\ Dados \ Documents \ Visual Studio 2015 \ Projects \ SQLite \ src \ SQLite \ Migrations \ Seed.cs:linha 36 em SQLite.Startup.CreateSampleData(IServiceProvider applicationServices)na F:\ Dados \ Documents \ Visual Studio 2015 \ Projects \ SQLite \ src \ SQLite \ Startup.cs:linha 138 em SQLite.Startup.Configure(IApplicationBuilder应用,IHostingEnvironment env,ILoggerFactory loggerFactory)na F:\数据\文档\ Visual Studio 2015 \项目\ SQLite \ src \ SQLite \ Startup.cs:第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数据库的。
我建议您还原并删除所有迁移,然后再次添加迁移。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句