2016-05-27 30 views
0

我首先使用數據庫。如何讓EF將SQL Server列識別爲計算?

我希望創建一個列,用於存儲記錄的日期時間LastModified。此列應該默認爲GetUTCDate(),並且當該行被修改時,設置爲GetUTCDate()

我可以做後者在桌子上使用觸發器。

然而,使用實體框架中插入一個記錄,在默認情況下它發送一個0日期在LastModified柱,並隨後在該列的默認約束將被忽略並且該值被設置爲0。

我時可以手動更改.edmx文件中列的StoreGeneratedPattern屬性。不過,我希望實體框架自動執行此操作 - 如果此操作成功,那麼我依靠內存來運行它。

有沒有什麼辦法來配置SQL Server中的列,以便實體框架永遠不會發送一個值時插入一條記錄(我相信這可以使用計算列實現)?

回答

1

您的EDMX只是一個XML文件。您可以創建一些簡單的控制檯應用程序,如EDMXFixer.exe,您可以在構建事件上運行並編輯您的文件。我們在所有表格中都有一些共同的列CreatedDate,默認值爲getdate()。所以我只是編輯EDMX文件並將所有這些列設置爲StoreGeneratedPattern = Computed

然後,我有這在我的預生成事件:

"$(ProjectDir)EDMXFixer.exe" "$(ProjectDir)DatabaseObjects\test.edmx" 

代碼定影液看起來是這樣的:

static void Main(string[] args) 
{ 
    int i; 
    int count; 

    XmlAttribute xmlAttribute; 

    if ((args == null ? false : (int)args.Length != 0)) 
    { 
     string str = args[0]; 
     bool flag = false; 

     if (File.Exists(str)) 
     { 
      FileInfo fileInfo = new FileInfo(str); 
      if ((fileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) 
      { 
       fileInfo.Attributes = (FileAttributes)(Convert.ToInt32(fileInfo.Attributes) - Convert.ToInt32(FileAttributes.ReadOnly)); 
       flag = true; 
      } 

      XmlDocument xmlDocument = new XmlDocument(); 
      xmlDocument.Load(str); 

      if (xmlDocument.DocumentElement != null) 
      { 
       count = xmlDocument.DocumentElement.ChildNodes[1].ChildNodes[1].ChildNodes[0].ChildNodes.Count; 
       for (i = 0; i < count; i++) 
       { 
        if (xmlDocument.DocumentElement != null) 
        { 
         foreach (XmlNode childNode in xmlDocument.DocumentElement.ChildNodes[1].ChildNodes[1].ChildNodes[0].ChildNodes[i].ChildNodes) 
         { 
          if ((childNode.Name != "Property" ? false : childNode.Attributes != null)) 
          { 
           if ((childNode.Attributes["Name"].Value != "CreatedDate" ? false : childNode.Attributes["Type"].Value == "datetime")) 
           { 
            xmlAttribute = xmlDocument.CreateAttribute("StoreGeneratedPattern"); 
            xmlAttribute.Value = "Computed"; 
            childNode.Attributes.Append(xmlAttribute); 
           } 
          } 
         } 
        } 
       } 
      } 
      if (xmlDocument.DocumentElement != null) 
      { 
       count = xmlDocument.DocumentElement.ChildNodes[1].ChildNodes[3].ChildNodes[0].ChildNodes.Count; 
       for (i = 0; i < count; i++) 
       { 
        if (xmlDocument.DocumentElement != null) 
        { 
         foreach (XmlNode xmlNodes in xmlDocument.DocumentElement.ChildNodes[1].ChildNodes[3].ChildNodes[0].ChildNodes[i].ChildNodes) 
         { 
          if ((xmlNodes.Name != "Property" ? false : xmlNodes.Attributes != null)) 
          { 
           if ((xmlNodes.Attributes["Name"].Value != "CreatedDate" ? false : xmlNodes.Attributes["Type"].Value == "DateTime")) 
           { 
            xmlAttribute = xmlDocument.CreateAttribute("annotation", "StoreGeneratedPattern", "http://schemas.microsoft.com/ado/2009/02/edm/annotation"); 
            xmlAttribute.Value = "Computed"; 
            xmlNodes.Attributes.Append(xmlAttribute); 
           } 
          } 
         } 
        } 
       } 
      } 
      xmlDocument.Save(str); 
      if (flag) 
      { 
       fileInfo.Attributes = (FileAttributes)(Convert.ToInt32(fileInfo.Attributes) + Convert.ToInt32(FileAttributes.ReadOnly)); 
      } 
     } 
    } 
} 

你只需要稍微改變此爲您的需求。

1

數據庫第一種情況:計算列是隻讀數據,您必須知道您不能寫入此列,與您必須知道要在數據庫的每列中寫入哪些數據的方式相同。

CODE第一種情況: 這裏計算列的在碼的aexample第一對象 普萊舍筆記註釋DatabaseGeneratedOption.Computed

public class UserProfile 
{ 
    public int Id { get; set; } 

    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)] 
    public string FullName { get; private set; } 
} 

需要就FullName屬性的屬性DatabaseGenerated。這是讓Entity Framework Code First知道數據庫將爲我們計算這個屬性的暗示。

+0

OP有數據庫第一個模型。 –

+0

是@GiorgiNakeuri我知道了,我寫了兩種情況 –

+0

如果你從數據庫更新你的'EDMX',你就失去了一切,必須重新做出這些改變。 –