Is the Entity Framework aware of identity columns?
实体框架是否知道标识列?
I am using SQL Server 2005 Express Edition and have several tables where the primary key is an identity column. when I use these tables to create an entity model and use the model in conjunction with an entity datasource bond to a formview in order to create a new entity I am asked to enter a value for the identity column. Is there a way to make the framework not ask for values for identity columns?
我使用的是SQL Server 2005 Express Edition,有几个表的主键是标识列。当我使用这些表创建一个实体模型,并使用模型与一个实体数据源绑定到一个formview时,为了创建一个新的实体,我被要求为identity列输入一个值。是否有一种方法可以让框架不为标识列请求值?
10 个解决方案
#1
67
I know this post is quite old, but this may help the next person arriving hear via a Google search for "Entitiy Framework" and "Identity".
我知道这篇文章已经很旧了,但是这可能会帮助下一个到达的人通过谷歌搜索“Entitiy Framework”和“Identity”听到。
It seems that Entity Frameworks does respect server-generated primary keys, as the case would be if the "Identity" property is set. However, the application side model still requires a primary key to be supplied in the CreateYourEntityHere
method. The key specified here is discarded upon the SaveChanges()
call to the context.
似乎实体框架确实尊重服务器生成的主键,就像设置了“Identity”属性一样。这里指定的键在SaveChanges()调用中被丢弃。
The page here gives the detailed information regarding this.
这里的页面提供了有关这方面的详细信息。
#2
5
If you are using Entity Framework 5, you can use the following attribute.
如果您正在使用Entity Framework 5,您可以使用以下属性。
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
#3
4
You should set the identity columns' identity specification so that the (Is Identity) property is set to true. You can do this in your table designer in SSMS. Then you may need to update the entity data model.
您应该设置标识列的标识规范,以便(Is identity)属性设置为true。可以在SSMS中的表设计器中执行此操作。然后您可能需要更新实体数据模型。
Perhaps that what you mean by saying the "Primary key is an identity column," or perhaps you missed this step.
也许这就是你所说的“主键是标识列”的意思,或者你错过了这一步。
#4
3
This is the best answer I've seen. You have to manually edit the storage layer xml to set StoreGeneratedPattern="Identity"
on each primary key of type UniqueIdentifier
that has the default value set to NewID()
.
这是我见过的最好的答案。您必须手工编辑存储层xml,以便在类型UniqueIdentifier的每个主键上设置StoreGeneratedPattern="Identity",该主键的默认值设置为NewID()。
http://web.archive.org/web/20130728225149/http:/ /leedumond.com/blog/using-a-guid-as-an-entitykey-in-entity-framework-4/
#5
2
Entity Framework is aware and can handle identity columns.
实体框架是敏感的,可以处理标识列。
Your problem can be maybe not the EF itself but the generated formview of it. Try to delete the input for the identity column from the insert form and let's see what happens.
你的问题可能不是EF本身,而是生成的formview。尝试从插入表单中删除标识列的输入,让我们看看会发生什么。
#6
2
If all else fails before you rip out your hair - try deleting your EntityModel and re-importing from SQL Server. If you've been tweaking the keys and relationships and relying on the 'update model from database' function it's still a bit buggy in the RC version I've found - a fresh import may help.
如果所有这些都失败了,那么在您删除您的头发之前——尝试删除您的EntityModel并从SQL Server重新导入。如果您一直在调整键和关系,并依赖于“数据库更新模型”功能,那么在我发现的RC版本中仍然存在一些问题——一个新的导入可能会有所帮助。
#7
2
In C# you can do something like this to make it aware:
在c#中,你可以这样做来让它意识到:
In your FooConfiguration.cs : EntityTypeConfiguration<Foo>
:
在你FooConfiguration。cs:EntityTypeConfiguration < Foo >:
this.Property(x => x.foo).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Then to use it, just be sure to insert the item into the context and call context.SaveChanges()
before using x.foo
to get the updated auto-incremented value. Otherwise x.foo
will just be 0 or null.
然后要使用它,只需确保在使用x之前将项目插入上下文并调用context. savechanges()。foo获取更新后的自动递增值。否则x。foo只是0或null。
#8
1
Entity framework does not fully understand Identities for some reason. The correct workaround is to set the Setter for that column to Private. This will make any generated UI understand that it should not set the identity value since it is impossible for it to set a private field.
由于某些原因,实体框架不能完全理解身份。正确的解决方案是将该列的Setter设置为Private。这将使任何生成的UI理解它不应该设置标识值,因为它不可能设置私有字段。
#9
0
I cannot believe it. Intellisensing ItemCollection yield the single item with ID = 0 after SaveChanges.
我不能相信它。智能感知项集合在保存更改后生成ID为0的单个项。
Dim ItemCollection = From d In action.Parameter
Select New STOCK_TYPE With {
.Code = d.ParamValue.<Code>.Value,
.GeneralUseID = d.ParamValue.<GeneralUse>.Value,
}
GtexCtx.STOCK_TYPE.AddObject( ItemCollection.FirstOrDefault)
GtexCtx.SaveChanges()
No matter what I do. After 8 hours including deleting my model, 35 times building and rebuilding, experimenting and editing the XML of EDMX and now almost coming to deleting my whole SQL Server database. At the 36th compile, this dumbfounding solution worked
不管我做什么。在删除我的模型、35次构建和重建、实验和编辑EDMX的XML之后,现在几乎要删除我的整个SQL Server数据库了。在第36次编译时,这个愚蠢的解决方案成功了
Dim abc = ItemCollection.FirstOrDefault
GtexCtx.STOCK_TYPE.AddObject(abc)
GtexCtx.SaveChanges()
abc.ID yield 41 (the identity i needed)
美国广播公司(abc)。ID收益率为41(我需要的身份)
EDIT: Here's a simple code for thought for looping AddObject and still get ID
编辑:这里有一个简单的代码用于循环AddObject并仍然获得ID
Dim listOfST As List(Of STOCK_TYPE) = ItemCollection.ToList()
For Each q As STOCK_TYPE In listOfST
GtexCtx.STOCK_TYPE.AddObject(q)
Next
GtexCtx.SaveChanges()
...more code for inter-relationship tables
Try Intellisence listOfST after SaveChanges and you will find updated ID. Maybe there's better way but the concept is there
在保存更改后尝试使用Intellisence listOfST,您会发现更新后的ID
#10
0
What worked for me was setting the StoreGeneratedPattern to None, when it was an Identity column. Now it all works consistently. The main problem with this is editing the models is an extreme chore if you have many models.
为我工作的是将StoreGeneratedPattern设置为None,当它是Identity列时。现在,这一切都是一致的。这样做的主要问题是,如果您有许多模型,那么编辑模型是一项非常繁琐的工作。
#1
67
I know this post is quite old, but this may help the next person arriving hear via a Google search for "Entitiy Framework" and "Identity".
我知道这篇文章已经很旧了,但是这可能会帮助下一个到达的人通过谷歌搜索“Entitiy Framework”和“Identity”听到。
It seems that Entity Frameworks does respect server-generated primary keys, as the case would be if the "Identity" property is set. However, the application side model still requires a primary key to be supplied in the CreateYourEntityHere
method. The key specified here is discarded upon the SaveChanges()
call to the context.
似乎实体框架确实尊重服务器生成的主键,就像设置了“Identity”属性一样。这里指定的键在SaveChanges()调用中被丢弃。
The page here gives the detailed information regarding this.
这里的页面提供了有关这方面的详细信息。
#2
5
If you are using Entity Framework 5, you can use the following attribute.
如果您正在使用Entity Framework 5,您可以使用以下属性。
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
#3
4
You should set the identity columns' identity specification so that the (Is Identity) property is set to true. You can do this in your table designer in SSMS. Then you may need to update the entity data model.
您应该设置标识列的标识规范,以便(Is identity)属性设置为true。可以在SSMS中的表设计器中执行此操作。然后您可能需要更新实体数据模型。
Perhaps that what you mean by saying the "Primary key is an identity column," or perhaps you missed this step.
也许这就是你所说的“主键是标识列”的意思,或者你错过了这一步。
#4
3
This is the best answer I've seen. You have to manually edit the storage layer xml to set StoreGeneratedPattern="Identity"
on each primary key of type UniqueIdentifier
that has the default value set to NewID()
.
这是我见过的最好的答案。您必须手工编辑存储层xml,以便在类型UniqueIdentifier的每个主键上设置StoreGeneratedPattern="Identity",该主键的默认值设置为NewID()。
http://web.archive.org/web/20130728225149/http:/ /leedumond.com/blog/using-a-guid-as-an-entitykey-in-entity-framework-4/
#5
2
Entity Framework is aware and can handle identity columns.
实体框架是敏感的,可以处理标识列。
Your problem can be maybe not the EF itself but the generated formview of it. Try to delete the input for the identity column from the insert form and let's see what happens.
你的问题可能不是EF本身,而是生成的formview。尝试从插入表单中删除标识列的输入,让我们看看会发生什么。
#6
2
If all else fails before you rip out your hair - try deleting your EntityModel and re-importing from SQL Server. If you've been tweaking the keys and relationships and relying on the 'update model from database' function it's still a bit buggy in the RC version I've found - a fresh import may help.
如果所有这些都失败了,那么在您删除您的头发之前——尝试删除您的EntityModel并从SQL Server重新导入。如果您一直在调整键和关系,并依赖于“数据库更新模型”功能,那么在我发现的RC版本中仍然存在一些问题——一个新的导入可能会有所帮助。
#7
2
In C# you can do something like this to make it aware:
在c#中,你可以这样做来让它意识到:
In your FooConfiguration.cs : EntityTypeConfiguration<Foo>
:
在你FooConfiguration。cs:EntityTypeConfiguration < Foo >:
this.Property(x => x.foo).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Then to use it, just be sure to insert the item into the context and call context.SaveChanges()
before using x.foo
to get the updated auto-incremented value. Otherwise x.foo
will just be 0 or null.
然后要使用它,只需确保在使用x之前将项目插入上下文并调用context. savechanges()。foo获取更新后的自动递增值。否则x。foo只是0或null。
#8
1
Entity framework does not fully understand Identities for some reason. The correct workaround is to set the Setter for that column to Private. This will make any generated UI understand that it should not set the identity value since it is impossible for it to set a private field.
由于某些原因,实体框架不能完全理解身份。正确的解决方案是将该列的Setter设置为Private。这将使任何生成的UI理解它不应该设置标识值,因为它不可能设置私有字段。
#9
0
I cannot believe it. Intellisensing ItemCollection yield the single item with ID = 0 after SaveChanges.
我不能相信它。智能感知项集合在保存更改后生成ID为0的单个项。
Dim ItemCollection = From d In action.Parameter
Select New STOCK_TYPE With {
.Code = d.ParamValue.<Code>.Value,
.GeneralUseID = d.ParamValue.<GeneralUse>.Value,
}
GtexCtx.STOCK_TYPE.AddObject( ItemCollection.FirstOrDefault)
GtexCtx.SaveChanges()
No matter what I do. After 8 hours including deleting my model, 35 times building and rebuilding, experimenting and editing the XML of EDMX and now almost coming to deleting my whole SQL Server database. At the 36th compile, this dumbfounding solution worked
不管我做什么。在删除我的模型、35次构建和重建、实验和编辑EDMX的XML之后,现在几乎要删除我的整个SQL Server数据库了。在第36次编译时,这个愚蠢的解决方案成功了
Dim abc = ItemCollection.FirstOrDefault
GtexCtx.STOCK_TYPE.AddObject(abc)
GtexCtx.SaveChanges()
abc.ID yield 41 (the identity i needed)
美国广播公司(abc)。ID收益率为41(我需要的身份)
EDIT: Here's a simple code for thought for looping AddObject and still get ID
编辑:这里有一个简单的代码用于循环AddObject并仍然获得ID
Dim listOfST As List(Of STOCK_TYPE) = ItemCollection.ToList()
For Each q As STOCK_TYPE In listOfST
GtexCtx.STOCK_TYPE.AddObject(q)
Next
GtexCtx.SaveChanges()
...more code for inter-relationship tables
Try Intellisence listOfST after SaveChanges and you will find updated ID. Maybe there's better way but the concept is there
在保存更改后尝试使用Intellisence listOfST,您会发现更新后的ID
#10
0
What worked for me was setting the StoreGeneratedPattern to None, when it was an Identity column. Now it all works consistently. The main problem with this is editing the models is an extreme chore if you have many models.
为我工作的是将StoreGeneratedPattern设置为None,当它是Identity列时。现在,这一切都是一致的。这样做的主要问题是,如果您有许多模型,那么编辑模型是一项非常繁琐的工作。