在实体框架中创建自定义属性
我有一个数据库,我想从中创建一个实体,然后生成 RESTful 输出。
我的目标是在其中一个表成为实体后将属性添加到其中。该属性的数据将是我通过对表中几个不同字段进行计算得出的数据。从那里,代码生成器将像平常一样创建 RESTful 输出。
我成功地能够更新 SSDL、CSDL 和 edmx 文件的映射部分,并使用 SampleEdmxCodeGenerator 作为自定义工具。当我用自定义属性填写 edmx 文件中的所有部分时,svc 失败,因为(我假设)数据库中不存在该属性。如果我将该属性保留在 SSDL 之外,而是将其放入客户端架构 (CSDL) 和映射部分,我将无法构建我的项目。
我已经修改了部分类并添加了它,但问题是我需要在类的创建时填充方法,但我还无法做到这一点。
我的方向是否正确,或者这是不可能的?看起来我应该能够以最小的努力做到这一点,但我总是碰壁。
I have a database I'd like to create an entity from, and then generate RESTful output.
My objective is to add a property to one of the tables once it becomes an entity. The data for that property would be one I'd come up with through calculations done on a few different fields in the table. From there, the code generator would create RESTful output like it normally does.
I have managed to be able to update the SSDL, CSDL, and the mapping sections of the edmx file along with using the SampleEdmxCodeGenerator as a custom tool. When I have all the sections in the edmx file filled out with my custom property, the svc fails because (I'm assuming) the property doesn't exist in the database. If I leave the property out of the SSDL, but put it in the client schema (CSDL) and the mapping section, I can't build my project.
I've modified the partial class and added to it, but the problem there is that I need to populate the methods on the creation time of the class, and I haven't been able to do that yet.
Am I headed in the right direction, or is this not possible? It seems like I should be able to do this with minimal effort, but I keep hitting walls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你正在走弯路才能到达你想要的地方。我(最近)没有使用过这两种方法,因此它们可能无法完全满足您的需求,但您可以尝试以下操作:
get
进行计算。I think you're taking detours to get where you want. I haven't used either of these approaches (recently), so they might not do exactly what you're after, but you could try this:
get
.部分类和部分方法是我答案的第一部分。我本质上想做的事却做不到。我可以操作使用部分方法和部分类返回的数据。我可以插入 OnmethodnameChanged() 方法来按照我希望的显示方式格式化数据,但这只能让我获得所需结果的一部分。
我还想做的是创建一个属性 c,它不作为数据库中的列存在(因此在我的实体中不存在),它是根据数据库中的几个不同属性(例如 a 和 b)计算得出的),然后将属性c添加到实体框架类中。在这样做时,我认为它会生成到 RESTful Web 服务输出中。
出现的问题来自于类需要更新您所做的任何更改,并将其传播回数据源。我不在乎这一点,因为我希望我的财产是只读的。根据我收集的信息,这是不可能的。
作为参考,这两篇文章确实有帮助:
向实体框架类添加自定义属性
(我目前只能发布一个网址,所以这是另一篇文章的地址)
social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/b7a9e01d-c5c2-4478-8f01-00f7f6e0f75f
我有什么我决定做的是公开我的实体,就像我到目前为止所做的那样,然后使用 RESTful 服务来操作数据并重新格式化它,并引入所需的属性。我会将结果转换为我自己的数据对象,并将其用作由另一个 RESTful Web 服务公开的数据源。我认为这个网站提供了一个关于如何公开自定义数据源的很好的例子。
mstecharchitect.blogspot.com/2008/12/surface-custom-data-source-in-adonet.html
如果由于某种原因速度太慢,我想我可以在数据库中创建另一个表来重新处理数据,以及我正在寻找的格式的计算输出。我想要避免的事情是让我的客户端必须进行任何数据操作,因为它将在一些微型设备上进行,例如手掌、iPhone 和黑莓。
希望能帮助其他遇到同样问题的人。这似乎是当前版本的数据服务的一个缺陷,但在某种程度上,我确信他们会在以后的版本中解决这个问题。也许 T4 和 .net 4.0 将解决这个问题。我不知道。
Partial Classes and Partial methods were the first part of my answer. What I'm essentially trying to do I can't do. I can manipulate data that is returned by using partial methods and partial classes. I can plug the OnmethodnameChanged() method to format the data how I'd like it to be shown, but that only gets me part way to my desired result.
What I would also like to do, is create a property c, which doesn't exist as a column in the database (and therefore does not exist in my entity), calculated from a couple different properties in the database (say a and b), and then add property c to the entity framework class. In doing this, I figured it would then get generated into the RESTful webservice output.
A problem that occurs comes from the need for the class to update any changes you make, and have it propagate back to the data source. I didn't care about that, because I want my property to be read only. From what I've gathered this isn't possible.
For reference, these two posts really helped:
Adding custom property to Entity Framework class
(I can only post one url currently, so here is the address to the other article)
social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/b7a9e01d-c5c2-4478-8f01-00f7f6e0f75f
What I've decided to do, is to expose my entity as I've done so far, then consume the RESTful service that manipulates data and reformats it, and introduces needed properties. I'll turn the results into my own data object, and use that as a datasource to be exposed by yet another RESTful web service. I think this website gives a good example on how to expose a custom datasource.
mstecharchitect.blogspot.com/2008/12/surfacing-custom-data-source-in-adonet.html
If for some reason that is too slow, I suppose I could just make another table in my database that has a reworking of the data, and the calculated output in a format I'm looking for. The thing I want to avoid is having my resulting client having to do any of the data manipulation since it will be on some micro devices like palms, iphones, and blackberries.
Hope that helps anyone else with the same problem. It seems that is a shortfall in the current version of Data Services, but to some extent, I'm sure they'll be addressing it in later versions. Maybe T4 and .net 4.0 will be addressing it. I'm not sure.