2016-05-27 5 views
0

私はまずデータベースを使用しています。EFにSQL Server列をComputedとして認識させる方法を教えてください。

レコードのLastModified日時を格納する列を作成したいとします。この列のデフォルトはGetUTCDate()で、行が変更された場合はGetUTCDate()に設定します。

後者はテーブルのトリガーを使用して行うことができます。

しかし、それはLastModified列に0日付を送り、その後、列のデフォルトの制約は無視され、値が0

Iに設定されているデフォルトでは、レコードを挿入するエンティティフレームワークを使用した場合.edmxファイルの列のStoreGeneratedPatternプロパティを手動で変更できます。しかし、Entity Frameworkが自動的にこれを行うようにしたいと思います。もしこれがライブになるのであれば、私はそれが動作するためにメモリに頼っています。

レコードを挿入するときにエンティティフレームワークが決して値を送信しないように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 FIRST CASE:ここ コードで計算された列の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; } 
} 

DatabaseGenerated属性はFullNameプロパティに必要とされている pleseはメモしてオブジェクト。これは、Entity Framework Code Firstに、データベースがこのプロパティを計算することを知らせるヒントです。

+0

OPには、データベースの最初のモデルがあります。 –

+0

はい@GiorgiNakeuri知っていると私は両方の場合を書いた –

+0

データベースからあなたの 'EDMX'を更新すると、すべてを失ってしまって、それらの変更をもう一度行う必要があります。 –

関連する問題