SQL Server Compact不支持服务器生成的密钥和服务器生成的值

时间:2011-10-05 10:19:13

标签: c# sql wpf

所以我理解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都设置为主键,唯一的是,以及标识为false。我无法看到我做错了什么仍然会出现此错误,所有其他字段都设置为可以为空但我不认为这与此错误有任何关系......

1 个答案:

答案 0 :(得分:0)

找到解决方案,结果我的身份值无法手动编辑,我不得不重新生成数据库,首先通过模型创建它。

相关问题