我在实体框架总共n00b。已经安装Postgresql和Npgsql。只想从一个简单的DB测试开始,但我已经在表格的ID列中遇到了问题。实体框架使用Postgresql和Npgsql
我开始创建一个简单的表得分和手动其PK约束在postgres的(下面SQL)。我使用参数/ retrofitmodel运行edmgen2.exe。在列表中,我只检查包含Score表和edmgen2.exe,然后输出4个文件。我将这些文件复制到VS2010项目目录,并将edmx包含到项目中。当我打开它的图表时,ScoreID标有一个Key符号。我编写代码来插入一个简单的对象。它的工作原理执行的第一次,但下一次我:
{“ERROR:23505:重复键值违反唯一约束\” Score_PK \“”}
我期待在数据库中,和其中一个项目得到了ScoreID 0.对我来说,EF出于某种原因似乎试图创建ID为0的另一个对象,而不是增加ID值。它驱使我坚果,因为在我开始使用真正的db模型之前,这只是一个愚蠢的简单测试。
我曾尝试:
在图改变从没有StoreGeneratedPattern属性标识,并且还计算的。
注射这些值也为用于ScoreID的SSDL文件属性
我已附加了一些下面所涉及的代码。 请帮我一把!
表的创建:
CREATE TABLE "Score"
(
"ScoreID" integer NOT NULL,
"ScorePoint" integer,
CONSTRAINT "Score_PK" PRIMARY KEY ("ScoreID")
)
WITH (
OIDS=FALSE
);
ALTER TABLE "Score"
OWNER TO postgres;
保存方法:
using (BoringDBContext dbContext = new BoringDBContext())
{
Score aScore = new Score();
aScore.ScorePoint = 9;
dbContext.AddToScore(aScore);
dbContext.SaveChanges();
}
SSDL FILE(除去<>):EDMX文件的
?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(除去<>):
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
听起来像你重置序列,一旦数据已经在,因此重复的关键错误 – markmnl 2012-05-15 08:45:27
不,重置的问题,问题是,列必须拥有序列,这与EF工作。 – VoidMain 2013-01-19 19:39:22