实体框架实体的计算值
在我的项目中,我有一个名为 Product 的实体,其中包含属性 Price 我需要计算每种产品的税金,因此我制作了一个模型定义的函数,如本教程中所示 http://blogs.msdn.com /b/efdesign/archive/2009/01/07/model-defined-functions.aspx
一切正常,生成的 SQL 很干净,但我有一个问题。
我需要为我刚刚创建的实体(未存储在数据库中的产品对象)获取税款。 是否可以对这样的对象使用模型定义的函数? 如果没有,我如何制定一种方法,使用 linq 和纯 C# 代码中的相同方法来计算税款?
提前致谢。
In my project, i have an entity named Product containing a property Price
I need to calculate the taxes for each product so i made a Model defined function like in this tutorial
http://blogs.msdn.com/b/efdesign/archive/2009/01/07/model-defined-functions.aspx
Everything works fine, Generated SQL is clean but i have a problem.
I would need to get the taxes for an entity i've just made (an Product object not stored in database).
Is it possible to use a model defined function for an object like this ?
If not, how Can I make a method to calculate the taxes with the same method from linq and from pure C# code ?
Thank's by advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我要做的是在 SQL Server 中创建一个用户定义的函数,使用它来填充计算列,然后只需从应用程序中调用它来填充新创建对象的
tax
值中的值。不幸的是,上次我检查时,在 EF4 中调用 UDF 相当笨拙,所以我所做的(我知道这不是世界上最干净的事情)是添加一个 LINQ-to-SQL 数据上下文,将 UDF 添加到那个,然后只需调用 UDF,就好像它是那里的 C# 方法一样。
为了让事情变得更清晰,我又走了一步,将相同的方法定义添加到我的 EF4 ObjectContext(通过部分类手动),然后在我的 EF 上下文上调用该方法,然后该上下文在 EF4 ObjectContext 的实例上调用该方法L2S 上下文。
我知道这看起来很多,但实际上并不算太糟糕。在 EF4 中调用 UDF 是不如 L2S 方便的一件事。
What I would do is create a user-defined function in SQL Server, use that to fill your computed column, then simply call it from your application to fill the value in your newly-created object's
tax
value.Unfortunately, last time I checked, it's rather clumsy calling a UDF in EF4, so what I did, and I know this isn't the cleanest thing in the world, is add a LINQ-to-SQL data context, add the UDF to that, and then just call the UDF as though it were a C# method from there.
To make things a bit cleaner, I went one more step and added the same method definition to my EF4 ObjectContext (by hand, via a partial class) and then called the method on my EF context, which then called the method on an instance of the L2S context.
I know it seems like a lot, but it's not too bad at all in practice. Calling UDFs in EF4 is the one thing that's not quite as convenient as L2S.