views:

72

answers:

3

Hi! I'm writing a custom .NET MembershipProvider (not the built in one) and trying to update using Entity Framework. But of course i have no access to (Try)UpdateModel. How can i update it? Thanks in advance.

A: 

To do this with the default provider is a little complicated, however what would be much easier would be to create your own CustomMembershipProvider as outlined here:

Implementing A Membership Provider

As you can do this to your OWN account model, you can code the repository/DAL code however you choose, and use standard EF practices and conventions, allowing you to perform simple and strongly mapped operations such as UpdateModel.

JTrott
What i'm trying to do is writing my own Membership Provider, but I dunno how to update my EF entity from there.
Agares
Ah, what you need Sir. is a DAL layer, or a Repository, I strongly reccomend using this example from Dane Morgridge, he provides a T4 template, simply place this in the same folder as your EDMX model, and run, and it will generate a data repository for your model (or at least the bones of one that you can then customise).http://geekswithblogs.net/danemorgridge/Default.aspx
JTrott
@Agares, writing your own Membership provider has nothing to do with updating the EF entity - this is a standard EF technique, just have a look around and you'll find plenty of examples.
RPM1984
+2  A: 

You can't do this kind of thing with the ASP.NET Membership Provider, that is, write custom updates to the tables.

If it were that easy, less people would have issues/problems with it. =)

Don't even bother adding the ASP.NET Membership SQL Tables onto your EDMX - you won't know the relationships or how the tables really work together. Forget about trying to represent it as a "Model".

My advice is don't try and bind to the MembershipProvider as a Model (i.e dont create a strongly typed view), just call the Membership methods directly from your controller.

This is where we start to miss the 'drag and drop' of Web Forms, can't drop on a ChangePassword control. =)

Your best bet would be to create a regular view (not strongly typed), then have regular buttons that post to your controller methods.

Don't try and pass through the object as a model, get the fields in the Request.Form collection.

[HttpPost]
public ActionResult ChangePassword()
{
   string userName = Request.Form["userName"];
   string passWord = Request.Form["passWord"];
   MembershipProvider.ChangePassword(userName, password);       
   return View("ChangePasswordSuccess");
}

The above code would be (roughly) the equivalent of passing through a strongly typed User object, changing the password and calling UpdateModel.

Of course, you could implement your own membership provider, but i dont believe implementing a custom provider just to make your code "easier" should be the driver, because unless coded properly (which is not easy to do), you compromise a lot of the built-in security features and wealth of account management options of the ASP.NET Membership provider that we take for granted.

RPM1984
+1. [Don't map the membership tables](http://blogs.teamb.com/craigstuntz/2010/03/05/38558/)
Craig Stuntz
A: 

A similar question was asked here.

Here is a CodeProject sample app that could get you started that uses EF and Microsoft's MembershipProvider. There is a class they built that inherits from MembershipProvider.
http://www.codeproject.com/KB/web-security/EFMembershipProvider.aspx

Jay