MVC3 + EF4.1 Code First, but using .NET Memebership - eh?

I am currently studying MVC3, EF4.1 and really love the Code First approach.

However, I want to use the built-in .NET Memembership Provider and the created DB.

What I can’t work is how I encode my domain to associate it in the membership database, and also how to get the database to generate through EF. I can run the appropriate scripts to create the database, but then it does not work and really does not feel anyway.

I do not expect a complete answer, I just need to indicate the direction of the textbook, which explains how this can be done.

If the only answer is to implement my own membership provider, then fine, but then again - does it feel like a lot of unnecessary work, given that MVC comes with an implementation out of the box?

Hello,

Wayne

+4
source share
3 answers

if your membership tables are in a different database than your domain tables, then you cannot use one DbContext in EF. What you can do here is that you can create two contexts, for example SecurityContext and DomainContext for each of these databases. But you will not be able to query by attaching objects that are in each of these contexts, and both contexts will not use the same transaction when updating the database (i.e. Not one unit of work).

I suggest you create these membership tables in your database instead of two databases. Then you can use one DbContext here (or event 2 if they are completely independent). Here, if you have correctly modeled all tables, columns, and relationships, you can generate a complete database using EF. But if I remember SqlMembershipProvider correctly, use stored procedures. If so, it will be difficult for you to create the entire database.

If you can provide more information, it’s easier to give a more specific solution.

0
source

I did to create a custom MemberhipProvider using the rules set by Microsoft (http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx); however, I use the first vice code model first. I created the tables in the designer and then created my database this way. After my structure was sound, I created MemberhipProvider, performing only those functions that I need as needed, largely throwing exceptions for those that I did not use.

If this does not work for you, you can use something similar to paragraph 2 in @Eranga's answer, but maybe use the database preparation steps used here: http://www.brianhawkinstech.com/development/120-preparing -your-ms-sql-database-for-use-with-aspnet-membership-provider . This would create the stored procedures needed for the SqlMemberhipProvider. I know that it will work with the first approach to the database, but I'm not sure how it will work with the first approach to the code.

The difficulty of using the approach in paragraph 2 above is explained by why I used the user membership approach. One of the nice things about this approach is that you can reuse the code in your future projects, but of course, this is a time-consuming approach.

0
source

If you want to implement your own membership and role provider, which I recommend, then here is a great tutorial for it: http://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and- MembershipProvider-in-MVC-3.aspx

0
source

All Articles