在动态数据中设置服务字段值
在我的项目中,许多表通过 ApplicationId 外键链接到 aspnet_Application 表。 我不希望用户查看或编辑它,所以我正在寻找在执行插入查询之前预设此 sql 表字段值的方法。 我仍然为该列启用了脚手架(以便 DD 生成正确的 sql 脚本),但我在所有编辑/列表/插入页面中隐藏了该列/字段。
理想情况下,我正在寻找一个在对 LinqToSql 类中的任何表执行 DynamicInsert 之前注入代码的位置。
谢谢
In my project many tables are linked to aspnet_Application table by ApplicationId foreign key. I don't want the users to view or edit it, so I'm looking for way to preset this sql table field value before the insert query is executed. I still have scaffolding enabled for this column (in order for DD to generate the right sql script) but I'm hiding this column/field in all my edit/list/insert pages.
Ideally I'm looking for a place to inject my code right before DynamicInsert is executed for any table in my LinqToSql class.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,经过一番挖掘后,我想出了一个可以接受的解决方案。 我为数据上下文创建了一个分部类,并为链接到 aspnet_Applications 的每个表添加了一个分部 Insert_ 方法。 在每个方法中,我将 ApplicationId 字段设置为当前应用程序 ID。
它看起来像这样:
请注意,在此方法中无法执行其他 LINQ 语句。 特别是,我必须将应用程序 ID 存储在会话中,而不是查询 aspnet_Applications 表。
虽然这是可以接受的解决方案,但它并不完美(有很多重复代码),所以如果有人知道的话最好在这里给我一根骨头:)
So after some digging around I came up with an acceptable solution. I've created a partial class for my data context and added a partial Insert_ method for every table that's linked to the aspnet_Applications. In every method I set the ApplicationId field to current application id.
It looks like this:
Note that you can't execute other LINQ statements while in this method. In particular I had to store the application id in session rather than querying the aspnet_Applications table.
While this is acceptable solution it isn't perfect (a lot of repetitive code) so if anyone knows better throw me a bone here :)
将来最好的解决方案是使用刚刚在 MIX09 上发布的 .Net RIA 服务预览版的 DomainService 部分,请参阅此处的视频:
.NET RIA 服务 - 使用 Microsoft Silverlight 和 Microsoft ASP.NET 构建数据驱动的应用程序
Microsoft ASP.NET 4.0 数据访问:Web 表单的成功模式
第一个是从 Silverlight 的角度对 .net RIA 服务的介绍,但更多内容应用于 DD,第二个是 David Ebbo 在MIX 并展示了 DomainService 如何与 DD 一起工作,我认为这是前进的方向,因为您可以在 DomainService 中完成所有业务逻辑。
In the future the best solution would be to use DomainService part of .Net RIA Services preview just released at MIX09 see there videos here:
.NET RIA Services - Building Data-Driven Applications with Microsoft Silverlight and Microsoft ASP.NET
Microsoft ASP.NET 4.0 Data Access: Patterns for Success with Web Forms
The first is an introl to .net RIA Services from the point of view od Silverlight but more of it applied to DD the second is David Ebbo's presentation at MIX and shows how DomainService works with DD I think this is the way forward as you can do all your business logic here in the DomainService.