开发者

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

开发者 https://www.devze.com 2023-03-10 01:03 出处:网络
I\'m currently learning MVC3, EF4.1 and really like the Code First approach. However, I want to use the built-in .NET Memembership Provider and DB that is generated.

I'm currently learning MVC3, EF4.1 and really like the Code First approach.

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

What I can't work out, is how I code up my Domain to link into the Membership DB and also how to get the DB to generate via EF. I can run the appropriate scripts to generate the DB but then this doesn't work and really doesn't feel right anyhow.

I don't expect full on answers, I just really need pointing in the direction of a tutorial 开发者_运维百科that explains how this could be accomplished.

If the only answer is to implement my own Membership Provider then ok, but again - it feels like a lot of unnecessary work considering that MVC ships with an implementation all ready out of the box?

Regards,

Wayne


if your membership tables are in a different database than your domain tables then you can not use a single DbContext in EF. What you can do in here is that you can create two contexts say SecurityContext and DomainContext for each of those databases. But you will not be able to query by joining entities that reside on each of this contexts and both contexts will not use the same transaction when updating database(ie not a single unit of work).

I would suggest you create those membership tables in your database instead of two databases. Then you can use a single DbContext here(or event 2 if they are completely independent). Here if you modeled all tables, column and relationships correctly you can generate the full database using EF. But if I remember correctly SqlMembershipProvider use stored procedures. If so you will have bit trouble creating the entire database.

If you can provide more info it is easier to give a more specific solution.


What I did was to create a custom MembershipProvider using the rules set up by Microsoft (http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx); however, I use the model first vice the code first approach myself. I created the tables in the designer myself then generated my database that way. After my structure was sound then I created the MembershipProvider, implementing only the functions I needed as I needed them, pretty much throwing exceptions for those I did not use.

If that doesn't work for you then you could use something similar to paragraph 2 in @Eranga's answer, but possibly use the database prep 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 by the SqlMemberhipProvider as well. I know it would work with database first approach, but not sure how it would work with a code first approach.

The complexity of using the approach in para 2 above is why I used the custom membership approach. One nice thing about this approach is that you can then reuse the code in future projects of your own as well, but is a time consuming approach of course.


If you want to implement your own membership and role provider, which i will 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

精彩评论

暂无评论...
验证码 换一张
取 消