2010-08-17 31 views
1

我想用我的DataContext的CreateDatabase方法動態地創建一個數據庫。我已經手動創建了映射類並進行了測試。但是,一旦我添加表達式列(見下文),創建將失敗並出現SqlCeException,我無法找到確切的原因。是否有可能在LINQ to SQL中使用DataContext.CreateDatabase創建計算列?

/// <summary> 
    /// The sum of ratings for this document. 
    /// </summary> 
    [Column] 
    public Nullable<float> RatingSum { get; set; } 

    /// <summary> 
    /// The number of times the document was rated. 
    /// </summary> 
    [Column] 
    public Nullable<float> RatingCount { get; set; } 

    /// <summary> 
    /// Average rating as calculated from RatingSum and RatingCount 
    /// </summary> 
    [Column(AutoSync=AutoSync.Never, IsDbGenerated=true, Expression = "RatingSum * RatingCount", DbType="REAL")] 
    public Nullable<float> Rating { get; set; } 

的省略的DbType或將其更改爲FLOAT沒有幫助,AutoSync.Always並沒有幫助,省略了自動同步和IsDbGenerated屬性也不管用。我究竟做錯了什麼?或者這只是不支持動態創建的數據庫?

回答

0

Linq to Sql本身在創建整個數據庫方面本身很差,至少它不會創建indecies,所以你必須做的第一件事就是存儲SQL代碼,該代碼在你需要創建的時候執行並運行它一個數據庫。