生成关于Serverside实体框架的指导5? [英] Generate Guid on Serverside Entity Framework 5?
本文介绍了生成关于Serverside实体框架的指导5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在流畅的nhibernate中,这是简单的
Id(x => x.Id).GeneratedBy.GuidComb );
解决方案
如果要在服务器上生成密钥,只需在代码中执行此操作:
public class TestObject
{
public TestObject()
{
Id = Guid.NewGuid();
}
public Guid Id {get;组; }
}
如果您希望数据库生成密钥,则使用 DatabaseGenerated 属性:
public class TestObject
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id {get;组; }
}
如果您在使用顺序GUID后,那么没有简单的答案此时此刻。一些让您沿着正确道路的例子:
I am coming from a nhibernate background and I am wondering how can I generate the Guid automatically on the serer side and not make a round trip to make it on the database side?
In fluent nhibernate it is simple just
Id(x => x.Id).GeneratedBy.GuidComb();
解决方案
If you want to generate the key on the server, simply do this in code:
public class TestObject
{
public TestObject()
{
Id = Guid.NewGuid();
}
public Guid Id { get; set; }
}
If you want the database to generate the key, then use the DatabaseGenerated attribute:
public class TestObject
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
}
If you're after using sequential GUIDs, then there's no easy answer at the moment. Some examples which get you along the right road:
- Generating IDs in the SaveChanges method
- Calling your own NewGuid method
- Use a non-EF method to change the default value for the identity field from
NEWID()
toNEWSEQUENTIALID()
这篇关于生成关于Serverside实体框架的指导5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文