为实体框架和 SQL Server CE 中的实体对象生成整数标识主键
我听说这个问题在 SQL Server Compact Edition 4.0 CTP 中得到了修复
最近我刚刚接触 SQL Server CE 和实体框架,而 VS2010 尚未支持 SQL Server CE 4.0
我想我需要解决这个问题
我可以知道吗如何在实体对象的构造函数内生成整数类型身份主键
public partial class Book
{
public Book()
{
// SQL Server Compact does not support entities with server-generated keys or values when it is used
// with the Entity Framework. Therefore, we need to create the keys ourselves.
Id = // Generating a Integer Identity Id here
//similar to Guid.NewGuid();
}
}
非常感谢您的帮助。
I heard that this issue is fixed in SQL Server Compact Edition 4.0 CTP
As recently I just stepped into SQL Server CE and Entity Framework, and VS2010 not yet supporting SQL Server CE 4.0
I think I would need a work around for this issue
Can I know how to generate an Integer type Identity Primary Key inside the constructor of the Entity Object
public partial class Book
{
public Book()
{
// SQL Server Compact does not support entities with server-generated keys or values when it is used
// with the Entity Framework. Therefore, we need to create the keys ourselves.
Id = // Generating a Integer Identity Id here
//similar to Guid.NewGuid();
}
}
Your help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经将 VS2010 和 SQL Server CE 与 Entity Framework CTP4 Code-First 一起使用,并且没有任何问题。我已经使用 NuPack 安装了这两个东西(现在是 NuGet - http://nuget.codeplex.com/)并且将其与 ASP.NET MVC 3 Beta 应用程序一起使用。
NuGet 上 SQLCE 的最新版本是 4.0.8435.1,要将其与 EF 一起使用,您需要 SQLCE.EntityFramework 4.0.8435.1。
I've used VS2010 and SQL Server CE with Entity Framework CTP4 Code-First and it worked with no problems. I've installed both thing using NuPack (now NuGet - http://nuget.codeplex.com/) and used it with ASP.NET MVC 3 Beta application.
The latest version of SQLCE on NuGet is 4.0.8435.1 and to use it with EF you have SQLCE.EntityFramework 4.0.8435.1 on there.
我遇到了同样的问题,我取 Id 列的最大值,然后加一。如果您正在开发桌面应用程序,您应该注意以下链接中的问题:通过ADO.Net实体框架将数据插入SQL Compact时出现问题
I met same problem, I take max value of the Id column after that add one. If you are developing a desktop application you should mind problem in the following link:Problem when inserting data into SQL Compact by ADO.Net Entity Framework
这正是我正在寻找的:
// SQL Server Compact 不支持具有服务器生成的键或值的实体
// 使用实体框架。因此,我们需要自己创建密钥。
因此我们必须手动生成 id (int),而不是从 SQL Server Compact 自动生成。
This is exactly what I'm looking for:
// SQL Server Compact does not support entities with server-generated keys or values when it
// with the Entity Framework. Therefore, we need to create the keys ourselves.
so we have to generate our id (int) manually instead of auto generating it from SQL Server Compact.