【发布时间】:2015-04-13 15:05:05
【问题描述】:
我首先在使用 EF 4.1 和数据库的网站上工作。
我也在使用 aspnet 表,尤其是 aspnet_Users,它使用 Guid 作为主键。
因此,我的自定义 User 表也有一个 Guid 作为主键,它是 aspnet_Users id 的外键。
最近,我读到除非使用 newsequentialid(),否则使用 Guid 作为主键是个坏主意。
使用 Sql Server Management Studio,我已将所有主键的默认值更新为 newsequentialid(),并在我的 edmx 设计器中将 StoreGeneratedPattern 设置为 Identity。
但是,每当我尝试添加一个对象(例如 Item 具有 Guid 主键)时,它的 id 在数据库中保持为空(全为 0)。
public void CreateItem()
{
using (var uof = new UnitOfWork())
{
Item item = new Item();
itemRepo.Add(item);
uof.Commit();
}
}
还有添加方法:
public class RepositoryBase<TObject> where TObject : IEntity
{
protected CavalenaEntities entities
{
get { return UnitOfWork.Current.Context; }
}
public void Add(TObject entity)
{
entities.Entry(entity).State = System.Data.EntityState.Added;
}
}
我是不是忘记了什么?
虽然我为我的数据库的主键设置了默认值,但它们在 edmx 设计器中的Default Value 属性仍然是None。正常吗?
另一种解决方案是使用 int 作为主键而不是 Guid,但是我怎样才能在我的自定义用户表和使用 Guid 的aspnet_Users 之间建立链接?
非常感谢!
【问题讨论】:
标签: c# asp.net entity-framework ef-database-first newsequentialid