在模型优先方法中,如何使用结构注释将SQL类型设置为最新
是否可以通过实体框架设计器将类型设置为仅日期(而不是日期时间) 我环顾四周,找到的唯一答案是一年前MSDN论坛上的一篇帖子 我是新来的,我真的不理解他们谈论结构注释的说明在模型优先方法中,如何使用结构注释将SQL类型设置为最新,sql,entity-framework,sql-server-2008,entity-framework-4,entity,Sql,Entity Framework,Sql Server 2008,Entity Framework 4,Entity,是否可以通过实体框架设计器将类型设置为仅日期(而不是日期时间) 我环顾四周,找到的唯一答案是一年前MSDN论坛上的一篇帖子 我是新来的,我真的不理解他们谈论结构注释的说明 我可以浏览生成的SQL脚本并更改每一行,但我不想这样做…结构注释-很好。这是我第一次听说这个功能,但它很有效。我刚试过。我会尽量解释一下 结构注释只是添加到EDMX文件中的随机xml。EDMX文件实际上就是XML,它有4个部分——CSDL、MSL、SSDL和与设计器中定位元素相关的部分 CSDL描述实体和实体之间的关联(在
我可以浏览生成的SQL脚本并更改每一行,但我不想这样做…结构注释-很好。这是我第一次听说这个功能,但它很有效。我刚试过。我会尽量解释一下 结构注释只是添加到EDMX文件中的随机xml。EDMX文件实际上就是XML,它有4个部分——CSDL、MSL、SSDL和与设计器中定位元素相关的部分
- CSDL描述实体和实体之间的关联(在设计器中定义)
- SSDL描述表和关系
- MSL描述CSDL和SSDL之间的映射
edmx:copytosdl=“true”
属性。该属性由用于创建SSDL的T4模板识别,它只获取该元素并将其放置到SSDL。生成的SSDL如下所示:
<Schema Namespace="Model.Store" Alias="Self"
Provider="System.Data.SqlClient" ProviderManifestToken="2008"
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="ModelStoreContainer">
<EntitySet Name="UsersSet" EntityType="Model.Store.UsersSet" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="UsersSet">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="Login" Type="nvarchar(max)" Nullable="false" />
<Property Name="CreatedAt" Type="datetime" Nullable="false">
<custom:SqlType xmlns:custom="http://tempuri.org/custom">Date</custom:SqlType>
</Property>
</EntityType>
</Schema>
将模板文件复制到新位置(以便不修改原始位置),并用以下内容替换默认表创建:
-- Creating table '<#=tableName#>'
CREATE TABLE <# if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=tableName#>] (
<#
for (int p = 0; p < entitySet.ElementType.Properties.Count; p++)
{
EdmProperty prop = entitySet.ElementType.Properties[p];
#>
[<#=Id(prop.Name)#>] <#
if (prop.MetadataProperties.Contains("http://tempuri.org/custom:SqlType"))
{
MetadataProperty annotationProperty = prop.MetadataProperties["http://tempuri.org/custom:SqlType"];
XElement e = XElement.Parse(annotationProperty.Value.ToString());
string value = e.Value.Trim();
#>
<#=value#> <# } else { #> <#=prop.ToStoreType()#> <# } #> <#=WriteIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#><#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>
<#
}
#>
);
GO
这可能是我见过的EDMX中最高级和隐藏的功能。注释和自定义T4模板可以让您对类和SQL生成进行大量控制。我可以想象,当首先使用model或选择性地向生成的POCO类添加一些自定义属性时,使用它来定义例如数据库索引或唯一键
之所以如此隐藏,是因为VS中没有现成的工具支持来使用它。从NuGet look for TiraggoEdmx,它以一种非常好的方式提供来自EDMX文件的所有低级信息。看哇,这是一个很棒的解释。谢谢是的,TT模板是相当未知的,或者简单地说,没有足够的广告。最近,当我试图解决常见的优化锁定和时间戳列问题时,我偶然发现了它,然后又一次,当我真的想让DateTime+StoredGeneratedPatter=Identity使ma成为该列上的默认GetDate()时。。通过调整模板,这很简单。如果我想通过代码(即C#)默认添加CopyToSSDL,是否有一个属性可以用来实现这一点?如果其他人在处理此问题时遇到错误“TemplateDbConfiguration无法设置…”,只需重新启动VS。
<Schema Namespace="Model.Store" Alias="Self"
Provider="System.Data.SqlClient" ProviderManifestToken="2008"
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="ModelStoreContainer">
<EntitySet Name="UsersSet" EntityType="Model.Store.UsersSet" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="UsersSet">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="Login" Type="nvarchar(max)" Nullable="false" />
<Property Name="CreatedAt" Type="datetime" Nullable="false">
<custom:SqlType xmlns:custom="http://tempuri.org/custom">Date</custom:SqlType>
</Property>
</EntityType>
</Schema>
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\SSDLToSQL10.tt
-- Creating table '<#=tableName#>'
CREATE TABLE <# if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=tableName#>] (
<#
for (int p = 0; p < entitySet.ElementType.Properties.Count; p++)
{
EdmProperty prop = entitySet.ElementType.Properties[p];
#>
[<#=Id(prop.Name)#>] <#
if (prop.MetadataProperties.Contains("http://tempuri.org/custom:SqlType"))
{
MetadataProperty annotationProperty = prop.MetadataProperties["http://tempuri.org/custom:SqlType"];
XElement e = XElement.Parse(annotationProperty.Value.ToString());
string value = e.Value.Trim();
#>
<#=value#> <# } else { #> <#=prop.ToStoreType()#> <# } #> <#=WriteIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#><#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>
<#
}
#>
);
GO
-- Creating table 'UsersSet'
CREATE TABLE [dbo].[UsersSet] (
[Id] int IDENTITY(1,1) NOT NULL,
[Login] nvarchar(max) NOT NULL,
[CreatedAt] Date NOT NULL
);
GO