views:

177

answers:

2

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

+2  A: 

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:

public partial class MyDataContext
{
    partial void InsertMyTable(MyTable instance)
    {
     instance.ApplicationId = HttpContext.Current.Session["ApplicationId"] as Guid?;
 this.ExecuteDynamicInsert(instance);
    }
}

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 :)

Roman R.
A: 

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.

Wizzard