2

I want to use the entity framework code-first approach for an asp.net mvc application together with the standard membership provider. The registered users will have posts etc which will link to the user via the userid in the users table. I want the posts and the other objects that point to a user be POCO classes (not inherit from any ef class). But the membership tables are not code-first, so what would be the best way to set this up?

Edit: I found http://codefirstmembership.codeplex.com/. What I don't like though is that my own objects (blogposts etc) should also be included in the membership datacontext that they have built. I'd like to create another datacontext for my own objects to keep things neatly separate and maintainable, but there does not seem to be any option in EF to facilitate communication between different datacontexts.

4

1 回答 1

2

不幸的是,没有简单的解决方案。我会将 Membership 作为一个独立的数据库,并且不会尝试任何深度集成,例如为 Membership 对象生成 EF 实体。

作为一个简单的解决方案,我在我的 EF 模型中创建了一个 User 实体,它具有标准 ASP.NET User 的一些附加属性 - 例如 Address 等(您当然可以为此使用 Profiles,但这可能会带来很大的性能损失)。然后在 EF User 对象中有一个指向 ASP.NET 用户的外键。

然后计算由 ASP.NET Membership 跟踪的属性的属性(即 EF 用户对象向 ASP.NET Membership 提供程序查询属性,例如电子邮件以避免数据重复)。

请注意,这不是一个理想的解决方案,就像 Microsoft 更改了 ASP.NET Membership 架构一样,您的代码可能会中断,如此处所述

于 2012-07-14T23:30:29.043 回答