使用索引生成实体框架代码
有没有办法告诉 .NET 4 ADO.NET 实体的 SQL 生成器为特定列创建索引?
Is there a way to tell .NET 4 ADO.NET Entity's SQL Generator to create indexes for a specific column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
索引本身不受支持,但如果您前往 这篇文章您可以了解如何将此自定义功能添加到现有模板中。
在本文的示例中,EDMX 的 CSDL 中的新索引将如下所示:
但要使其正常工作,您必须修改一些内容(有关详细信息,请参阅我提供的链接)。首先,您必须在架构节点上声明“myExtensions”命名空间:
其次,您必须修改位于以下位置的模板:
该解决方案需要 Linq,因此将其添加到模板的顶部:
然后将其添加到底部:
大部分可以可以很容易地修改以满足您的需求。本文介绍了更多细节,但上述代码中最重要的一行是获取自定义“索引”扩展节点的行:
希望有所帮助!
Indices are not supported natively, but if you head to the "Influencing the DDL Generation" section of this article you can see how to add this custom functionality to an existing template.
In the article's example, the new index in your EDMX's CSDL would look something like this:
But to get this working you would have to modify a few things (see the link I provided for the details). Firstly, you'd have to declare that "myExtensions" namespace on the schema node:
Secondly, you'd have to modify the template found at:
The solution requires Linq, so add this to the top of the template:
And then add this to the bottom:
Most of that could be pretty easily modified to fit your needs. The article goes into more of the details, but the most important line in the above code is the one which fetches that custom "Index" extension node:
Hope that helps!
补充 Smudge 的答案,需要采取一些技巧才能在 EF 5 中工作。
例如,edmx:CopyToSSDL="true" 不能立即工作。您必须做一些修改:
然后在自定义属性中(注意 edmxv2):
请参阅此链接 了解更多信息。
另外,我确实更改了一些 T4 代码以使其更容易。我以这个示例作为工作基础来完成更灵活的自定义元素句法。
例如,您可以在 EntityType 元素末尾添加自定义元素(不必将其放入
标记内):然后修改 . tt模板:
Complementing Smudge's answer, there are some tricks that need to be done for this to work in EF 5.
For instance, edmx:CopyToSSDL="true" doesn't work right off the bat. You have to do some hacks:
And then in the custom property (notice the edmxv2):
See this link for more info.
Also, I did change some of the T4 code to make it more easy. I took this example as a working base to accomplish a more flexible custom elements syntax.
You could add a custom element at the end of the EntityType element for instance (you don't have to put it inside a
<Property></Property>
tag):And then modify the .tt template: