[英]Entity Framework using Postgresql and Npgsql
I am total n00b at Entity framework. 我在实体框架上总共n00b。 Have installed Postgresql and Npgsql.
已经安装了Postgresql和Npgsql。 Just wanted to begin with a simple test of DB, but I get problems already with the table's ID column.
只是想从简单的数据库测试开始,但是我已经在表的ID列中遇到了问题。
I start with creating a simple table Score and its PK constraint manually in postgres (sql below). 我首先在postgres中手动创建一个简单的表Score及其PK约束(下面的sql)。 I run edmgen2.exe with parameter /retrofitmodel.
我用参数/ retrofitmodel运行edmgen2.exe。 In the list i check only to include the Score table and edmgen2.exe then outputs 4 files.
在列表中,我只检查包括Score表和edmgen2.exe然后输出4个文件。 I copy the files to VS2010 project directory and also include the edmx to the project.
我将文件复制到VS2010项目目录,并将edmx包含到项目中。 When I open its diagram, the ScoreID is marked with a Key symbol.
当我打开它的图表时,ScoreID标有一个Key符号。 I write code to insert a simple object.
我编写代码来插入一个简单的对象。 It works the first time executed, but next time I get:
它在第一次执行时有效,但是下次我得到:
{"ERROR: 23505: duplicate key value violates unique constraint \\"Score_PK\\""} {“错误:23505:重复的键值违反了唯一约束\\“ Score_PK \\”“}
I look in the database, and the one item there has got ScoreID 0. To me it seems for some reason EF is trying to create another object with ID 0 instead of incrementing the ID value. 我查看数据库,其中一个项目的得分ID为0。在我看来,由于某种原因,EF试图创建另一个ID为0的对象,而不是增加ID值。 It drives me nut since this is only a stupid simple test before I get started with the real db model.
这让我发疯了,因为在我开始真正的数据库模型之前,这只是一个愚蠢的简单测试。
I have tried: 我努力了:
Changing StoreGeneratedPattern attribute from None to Identity in diagram, and also to Computed. 在图中将StoreGeneratedPattern属性从None更改为Identity,也更改为Computed。
Injecting those values also into the ssdl file for ScoreID attribute 将这些值也注入到ScoreID属性的ssdl文件中
I have attached some of the involved code below. 我在下面附上了一些涉及的代码。 Please help me out!!
请帮帮我!!
TABLE CREATION: 表创建:
CREATE TABLE "Score"
(
"ScoreID" integer NOT NULL,
"ScorePoint" integer,
CONSTRAINT "Score_PK" PRIMARY KEY ("ScoreID" )
)
WITH (
OIDS=FALSE
);
ALTER TABLE "Score"
OWNER TO postgres;
SAVE METHOD: 保存方法:
using (BoringDBContext dbContext = new BoringDBContext())
{
Score aScore = new Score();
aScore.ScorePoint = 9;
dbContext.AddToScore(aScore);
dbContext.SaveChanges();
}
SSDL FILE (removed <>): SSDL文件(已删除<>):
?xml version="1.0" encoding="utf-8"?
Schema Namespace="BoringDB.store" Alias="Self" Provider="Npgsql" ProviderManifestToken="9.1.2" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="BoringDBstoreContainer">
<EntitySet Name="Score" EntityType="BoringDB.store.Score" store:Type="Tables" Schema="public" /
/EntityContainer>
EntityType Name="Score"
Key
PropertyRef Name="ScoreID"
/Key
Property Name="ScoreID" Type="int4" Nullable="false" StoreGeneratedPattern="Identity"
Propert Name="ScorePoint" Type="int4"
/EntityType
/Schema
PART OF EDMX FILE (removed <>): EDMX文件的一部分(已删除<>):
edmx:ConceptualModels
Schema Namespace="BoringDB" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"
EntityContainer Name="BoringDBContext"
EntitySet Name="Score" EntityType="BoringDB.Score" /
/EntityContainer
EntityType Name="Score"
Key
PropertyRef Name="ScoreID" /
/Key
Property Name="ScoreID" Type="Int32" Nullable="false" a:StoreGeneratedPattern="Identity" xmlns:a="http://schemas.microsoft.com/ado/2009/02/edm/annotation" /
Property Name="ScorePoint" Type="Int32" Nullable="true" /
/EntityType
/Schema
/edmx:ConceptualModels
I found the short answer to my long quesion. 我找到了我长期问题的简短答案。 Changing ScoreID datatype to BIGSERIAL instead of integer in the database made the auto increment work.
在数据库中将ScoreID数据类型更改为BIGSERIAL而不是整数可以使自动增量工作。 Manually creating a sequence and setting it as Default value never did, don' know why.
手动创建一个序列并将其设置为默认值从来没有,不知道为什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.