【发布时间】:2011-10-05 10:19:13
【问题描述】:
所以我知道 CE 不支持服务器生成的值 - 没关系,我正在提供我自己的代码...
public static class ExtensionMethods
{
public static TResult NextId<TSource, TResult>(this ObjectSet<TSource> table, Expression<Func<TSource, TResult>> selector)
where TSource : class
{
TResult lastId = table.Any() ? table.Max(selector) : default(TResult);
if (lastId is int)
lastId = (TResult)(object)(((int)(object)lastId) + 1);
return lastId;
}
}
private void AddProperty()
{
Property p = new Property();
entities.AddToProperties(new Property()
{
ID = entities.Properties.NextId(u => u.ID),
AgentName = "Agent Name",
Address = "New Property",
AuctioneerName = "Auctioneer Name",
SaleTitle = "Sales Title",
DateOfAuction = null,
ReservePrice = null,
CurrentBid = null,
PreviousBid = null,
});
entities.SaveChanges();
BindData();
}
唯一的问题是,当我认为它应该解决问题时,这并不能解决问题。我有 2 张桌子。一个叫Property,另一个叫Image。两者的ID都设置为主键,唯一的yes,identity为false。我看不出我做错了什么才能得到这个错误,所有其他字段都设置为可为空,但我认为这与这个错误没有任何关系......
【问题讨论】:
-
你能发布错误信息吗?
-
SQL Server Compact 不支持服务器生成的键和服务器生成的值