XML 数据类型
如何将 EF 设置为对对象使用 XML 的 SQL 数据类型?此外,如何创建 SQL 计算列。
我们存储了大量动态数据,有些人会为每行/记录创建元数据,它不是标准的,因此我们依赖 xml 数据结构,然后使用计算列来创建键,以便我们可以用于更快的 SQL 搜索。
坦率地说,映射到 xml 列的 Expando 对象确实能让我们的船漂浮。
提前致谢。
How do I set EF to use an SQL datatype of XML for an object? Moreover, how does one create SQL computed columns.
We store alot of dynamic data that some folks cause meta data per row/record, it isn't standard and therefore we rely upon the xml data structure and then use computed columns to create so keys that we may use for faster SQL searches.
Quite frankly - an expando object mapping to an xml column would really float our boat.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原来的问题是:
在代码优先中,您可以这样做:
或者通过 Fluent api:
您仍然需要将属性解释为字符串,并且您需要自己在 C# 中将其转换为 xml。但这样它将生成为 xml 列,并且您仍然可以使用 sql 的 xml 函数对其执行直接 sql 查询。
The original question was:
In code first, you can do it like this:
Or through the fluent api:
You still need to interpret the property as a string and you're on your own for converting it to xml within C#. But this way it will get generated as an xml column and you can still perform direct sql queries against it using sql's xml functions.
您在 EF 中所能做的就是以字符串形式访问/读取 XML 列 - 从那时起,您就只能靠自己了。没有“内置”机制可以将该 XML 转换为序列化对象或类似的东西。
至于计算列:
您可以在 SQL 中定义一个简单的表达式
这将为您提供一个基于您的列
PriceWithoutTax
的新列PriceWithTax
和另一个名为TaxRate
的列(0.15 表示 15% 税)< /p>或者您可以创建一个返回单个值的存储函数,并调用它
我们还使用存储标量函数的这种方法来解析存储在 XML 列中的 XML,并将某些部分提取到计算列中。
如果 SQL Server 认为您的计算是“确定性”的,您还可以将
PERSISTED
关键字添加到列定义中。在这种情况下,您的值将被计算一次并实际持久/存储在该表中,就像任何其他列一样。您现在甚至可以为这些列建立索引!这对于例如从 XML 中抓取位并将其公开在“父”表上的标量函数非常有效。All you can do in EF is to access/read an XML column as a string - from there on out, you're on your own. There's no "built-in" mechanism to turn that XML into a serialized object or something like that.
As for computed columns:
you can define a simple expression in your SQL
This would give you a new column
PriceWithTax
based on your columnPriceWithoutTax
and another column calledTaxRate
(0.15 for 15% tax)or you can create a stored function that returns a single value, and call that
We also use this method of a stored scalar function to parse into the XML stored in an XML column, and extract certain bits and pieces into a computed column.
If your computation is deemed "deterministic" by SQL Server, you can also add a
PERSISTED
keyword to your column definition. In this case, your values are computed once and actually persisted / stored on that table, just like any other column. You can now even index those columns! This works great with e.g. scalar functions that grab bits from within an XML and expose it on the "parent" table.虽然不是您正在寻找的答案,但 NHibernate 完全支持您正在处理的场景类型。两种方法:
XDocument
或XmlDocument
属性。它开箱即用,您可以将其映射到xml
sql 类型、nvarchar(max)
等。此外,对于一般的计算属性,您可以查看
formula
和只读。
如果您的项目足够早并且需要这种灵活性,您可能会考虑切换。
While not the answer you're looking for, NHibernate fully supports the kind of scenarios you're dealing with. Two approaches:
XDocument
orXmlDocument
property in your domain model. It works out of the box and you can map it to anxml
sql type,nvarchar(max)
, etc.Also, for computed properties in general, you can take a look at
formula
andreadonly
.If you are early enough in your project and require this kind of flexibility, you might consider switching.