C# 如何让EF将SQL Server列识别为计算列?
我先用数据库 我希望创建一个列,用于存储记录的C# 如何让EF将SQL Server列识别为计算列?,c#,sql-server,entity-framework,timestamp,C#,Sql Server,Entity Framework,Timestamp,我先用数据库 我希望创建一个列,用于存储记录的LastModifieddatetime。此列应默认为GetUTCDate(),当修改行时,设置为GetUTCDate() 我可以用桌上的触发器来做后者 但是,当使用实体框架插入记录时,默认情况下,它在LastModified列中发送一个0日期,随后忽略该列上的默认约束,并将该值设置为0 我可以手动更改.edmx文件中列的StoreGeneratedPattern属性。然而,我希望实体框架能够自动做到这一点——如果这一点能够实现,那么我将依靠内存来工
LastModified
datetime。此列应默认为GetUTCDate()
,当修改行时,设置为GetUTCDate()
我可以用桌上的触发器来做后者
但是,当使用实体框架插入记录时,默认情况下,它在LastModified
列中发送一个0日期,随后忽略该列上的默认约束,并将该值设置为0
我可以手动更改.edmx文件中列的StoreGeneratedPattern属性。然而,我希望实体框架能够自动做到这一点——如果这一点能够实现,那么我将依靠内存来工作
有没有办法在SQL Server中配置列,以便Entity framework在插入记录时永远不会发送值(我相信这可以通过使用计算列来实现)?数据库第一种情况:计算列是只读数据,您必须知道您不能在该列中写入,正如您必须知道要在数据库的每一列中写入哪些数据一样 代码第一个案例: 这里是代码优先对象中计算列的一个示例 请注意注释
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属性。这是一个提示,让实体框架代码首先知道数据库将为我们计算此属性。数据库第一种情况:计算列是只读数据,您必须知道您不能在此列中写入,就像您必须知道在数据库的每一列中写入什么数据一样 代码第一个案例: 这里是代码优先对象中计算列的一个示例 请注意注释
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属性。这是一个提示,让实体框架代码首先知道数据库将为我们计算此属性。您的
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));
}
}
}
}
static void Main(字符串[]args)
{
int i;
整数计数;
XmlAttribute XmlAttribute;
if((args==null?false:(int)args.Length!=0))
{
字符串str=args[0];
布尔标志=假;
如果(File.Exists(str))
{
FileInfo FileInfo=新的FileInfo(str);
if((fileInfo.Attributes&FileAttributes.ReadOnly)=FileAttributes.ReadOnly)
{
fileInfo.Attributes=(FileAttributes)(Convert.ToInt32(fileInfo.Attributes)-Convert.ToInt32(FileAttributes.ReadOnly));
flag=true;
}
XmlDocument XmlDocument=新的XmlDocument();
加载(str);
if(xmlDocument.DocumentElement!=null)
{
count=xmlDocument.DocumentElement.ChildNodes[1]。ChildNodes[1]。ChildNodes[0]。ChildNodes.count;
对于(i=0;i