views:

420

answers:

2

I have a linq-to-sql class. I have a property "Password" for which I want to call the underlying ASP.NET Membership provider. Thus, I do not want this property written out directly but via my own code. I basically want to create a facade/proxy for this property such that I may use the underlying membership provider or a custom stored procedure.

I want to accomplish without modifying the LINQ-TO-SQL designer generated code, if at all possible.

A: 

It looks like it might be possible to create a custom DataContext to handle this situation.

http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx


There are partial methods for the individual properties as, well and an OnValidate method.

In my case, I think the best solution is to throw an exception in the property changing method and add a public method for setting this individual property. This solution while not perfect will avoid touching the SQL generated code, where the property could be set to readonly or the setter removed.

Other suggestions welcome.

Curtis White
A: 

It is possible. You can add your properties and methods to linq generated class using partial class mechanism. Linq generated classes are marked partial so you can add class members with:

public partial class YourLinqClass
{
  // your methods and properties. refer linq properites and methods with "this."
  // example:
  public string Password
  {
     get
     {
         int id = this.UserId;
         string password = // ... get password
         return password;
     }
     set
     {
         // ...
     }
  }
}

You have to place the partial class in the same namespace as the rest of dbml.

PanJanek
Poor answer, it is very general and does not answer the specific question.
Curtis White
@Curtis: As far as I understood your question, you want to add some code (property) to linq-generated class, and that is what I've explained how to do. I don't know the exact logic of password retrieving you use. I answered how to add proxy property to linq classes (I added some more code while ago to make it more understandable).
PanJanek
Yes, I understand how to do that. This question deals with how to modify a designer generated property, i.e a property that is generated already. As my own answer already stated, there are partial methods for this for each property. But, my problem goes beyond that in that I do not want the property persisted using the LINQ framework but do not want to modify the designer generated code either. Not touching the designer generated code is key because once we go that route then it changes the whole work flow.
Curtis White
Good answers would be: throw an exception using partial property and add a custom method. Create a custom datacontext and check the change set (possibly). Or one might even mention a third party tool that allows designer generated changes to be saved, such as Huggati. Instead you presented a solution to the trivial case.
Curtis White