2011-10-05 39 views
1

所以我明白,CE不支持服務器生成值 - 那好吧,我提供我自己的這個代碼...服務器生成的密鑰和服務器生成的值不支持SQL Server Compact

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(); 
     } 

唯一的問題是,這並不能解決問題,當我認爲它應該。我有兩張桌子。一個叫做Property,另一個叫Image。兩者的ID都設置爲主鍵,唯一的是,並且標識爲假。我看不出我在做什麼錯誤仍然得到這個錯誤,所有其他字段都設置爲可以爲空,但我不認爲這與這個錯誤有什麼關係...

+0

你能發佈錯誤信息嗎? – JohnD

+0

SQL Server Compact不支持服務器生成的密鑰和服務器生成的值 – randomalbumtitle

回答

0

找到解決方案,它轉身我的身份值無法手動編輯,我必須重新生成通過模型首先創建它的數據庫。