如何在模型优先方法中使用结构注释将 SQL 类型设置为 Date
是否可以通过实体框架设计器将类型设置为日期(而不是日期时间)?
我环顾四周,找到的唯一答案是一年前来自 MSDN 论坛的帖子...
我是新来的,我不太明白他们谈论结构注释的说明...
我可以浏览生成的 SQL 脚本并更改每一行,但我宁愿不这样做...
Is it possible to set type to just date (NOT datetime) via entity framework designer?
I had a look around and the only answer that I've found is a post from MSDN forum from a year ago...
I'm very new here and I don't really understand the instructions where they talk about structural annotations...
I can go through the generated SQL script and change each line but I rather not do that...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结构注释 - 很好。这是我第一次听说这个功能,但它确实有效。我刚刚尝试过。我会尝试稍微解释一下。
结构注释只是添加到 EDMX 文件中的随机 xml。 EDMX 文件实际上只是 XML,它有 4 个部分 - CSDL、MSL、SSDL 以及与设计器中的定位元素相关的部分。
如果您首先从模型开始(您想从模型生成数据库),则只有 CSDL 部分和 SSDL创建 SSDL 后,MSL 将由一些自动过程(在工作流中执行的 T4 模板)生成,另一个 T4 模板将生成用于数据库创建的 SQL 脚本。
链接的 MSDN 论坛主题中描述的结构注释是一个提示。您将结构注释放入 EDMX 的 CSDL 部分(您必须以 XML 形式打开 EDMX - 单击解决方案资源管理器中的文件并选择打开方式)。我的测试 CSDL 描述了具有三个属性的单个 User 实体(实体在答案后面的屏幕截图中可见):
我在
Schema
元素中添加了自定义命名空间定义:xmlns:custom="http:// /tempuri.org/custom"
并为CreatedAt
属性定义自定义结构注释:用于结构注释的命名空间或元素的名称并不重要 - 完全取决于您的名称你用吗?唯一重要的是
edmx:CopyToSSDL="true"
属性。此属性由用于 SSDL 创建的 T4 模板识别,它只是将此元素放入 SSDL。生成的 SSDL 如下所示:唯一的一点是将结构注释移至 SSDL。所有注释都可以通过某些名称值集合在元数据中访问。现在您需要修改负责 SQL 脚本生成的 T4 模板以识别此注释并使用注释中定义的值而不是属性中定义的类型。您可以在以下位置找到该模板:
将模板文件复制到新位置(这样就不会修改原始位置),并将默认表创建替换为:
现在最后一点是更改用于 SQL 脚本生成的模板。在设计器中打开 EDMX 文件并转到模型的属性(只需在打开属性窗口时单击设计器中的某个位置)。将 DDL Generation Template 更改为您修改的模板。
运行从模型生成数据库,它将创建包含
以下 内容的 SQL 脚本:可能是我见过的 EDMX 最先进、最隐藏的功能。注释与自定义 T4 模板一起可以让您对类和 SQL 生成进行大量控制。我可以想象在首先使用模型时使用它来定义数据库索引或唯一键,或者有选择地向生成的 POCO 类添加一些自定义属性。
之所以如此隐藏,是因为 VS 中没有开箱即用的工具支持来使用它。
Structural annotation - nice. It is the first time I heard about this feature but it works. I just tried it. I will try to explain it little bit.
Structural annotations are just random xml added to EDMX file. EDMX file is in fact just XML wich has 4 parts - CSDL, MSL, SSDL and part related to positioning elements in the designer.
If you start with model first (you want to generate database from your model), you have only CSDL part and both SSDL and MSL will be generated by some automatic process (T4 templates executed in workflow) once SSDL is created another T4 template will generate SQL script for database creation.
Structural annotation described in linked MSDN forum's thread is a hint. You will place structural annotation into CSDL part of the EDMX (you must open EDMX as XML - click on the file in solution explorer and choose Open with). My test CSDL describes single User entity with three properties (entity is visible on screenshot later in the answer):
I have added custom namespace definition in
Schema
element:xmlns:custom="http://tempuri.org/custom"
and defined custom structural annotation forCreatedAt
property:The name of the namespace or element used for structural annotation are not important - it is absolutely up to you what names do you use. The only important thing is
edmx:CopyToSSDL="true"
attribute. This attribute is recognized by T4 template used for SSDL creation and it just takes this element and places it to SSDL. Generated SSDL looks like:The only point was moving the structural annotation to SSDL. All annotations are accessible in metadata through some name value collection. Now you need to modify T4 template responsible for SQL script generation to recognize this annotation and use the value defined in the annotation instead of type defined in the property. You can find the template in:
Copy template file to new location (so that you don't modify the original one) and replace default table creation with this:
Now the last point is changing the template used for SQL script generation. Open EDMX file in the designer and go to model's properties (just click somewhere in the designer while you have properties window opened). Change DDL Generation Template to the template you modified.
Run Generate Database from Model and it will create SQL script containing:
This is probably the most advanced and hidden feature of EDMX I have seen yet. Annotations together with custom T4 templates can get you a lot of control over both class and SQL generation. I can imagine using this to define for example database indexes or unique keys when using model first or add selectively some custom attributes to generated POCO classes.
The reason why this is so hidden is that there is no tooling support in VS out-of-the box to use this.
从 NuGet 中查找 TiraggoEdmx,它以非常好的方式提供 EDMX 文件中的所有低级信息。请参阅 http://brewdawg.github.io/Tiraggo.Edmx/
From NuGet look for TiraggoEdmx, it serves up all the low level information from your EDMX files in a very nice way. See http://brewdawg.github.io/Tiraggo.Edmx/