使用默认成员关系的 MVC3 实体框架
我想在自定义表(网站)和与用户相关的默认 aspnet 表之间创建关系。
我使用的是代码优先,因此对于大多数 FK 关系,我只需
public ModelName ModelName { get; set; }
这样做,EF 将自动创建 FK 关系。很容易。
令人困惑的是连接到 aspnet 用户/成员资格表的最有效方法。我是否创建一个充当接口的新模型 Users 以便我可以实现自定义用户代码?
有没有一种适合 EF 最佳实践的最佳方法?我基本上只是想将用户与网站表/模型相关联,以便 EF 可以完成其工作。
I want to create a relationship between a custom table (Websites) and the default aspnet tables related to Users.
I'm using code-first so for most FK relationships I would just do
public ModelName ModelName { get; set; }
With this, EF will automatically create the FK relationships. Very easy.
What's confusing is the most effective way to hook into the aspnet users/membership table. Do I create a new model Users that acts as an interface so that I can implement custom user code?
Is there a best way to do this that fits well into EF best practices? I basically just want to relate a user to the Websites table/model so that EF can do its thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“我是否要创建一个充当接口的新模型 Users 以便我可以实现自定义用户代码?”
如果你想要灵活性,我会说这就是你要走的路。这样,如果您将来想更改为某种不同的身份验证数据库结构,会更容易。
例如,有一个“AppUser”实体,其中相应的表具有指向 aspnet_Membership 表的“UserID”列的外键。通过这种方式,您可以简单地向“AppUser”实体添加属性,而不必尝试更改 MS 表结构(这可能是一个真正的痛苦)。您仍然可以使用 MvcMembership 入门套件 DLL 之类的工具与 MVC 项目中的内置 MS Membership 类和函数进行交互。
https://github.com/TroyGoode/MembershipStarterKit
希望这有帮助!
"Do I create a new model Users that acts as an interface so that I can implement custom user code?"
If you want flexibility, I would say this is the way to go. This way it would be easier if you wanted to change to some sort of different Authentication DB structure in the future.
For example, have an "AppUser" Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way you can simply add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.
https://github.com/TroyGoode/MembershipStarterKit
Hope this helps!
这有几个前提条件:
如果您希望模型类与 ASP.NET 表有关系,则必须将 ASP.NET 表建模为另一个实体。我不确定您是否可以使用 ASP.NET 类来实现此目的,因为例如
MembershipUser
没有 EF 所需的无参数公共构造函数。因此,您很可能需要创建重复的类及其映射,并在引用 ASP.NET 实体时使用这些类。This has few preconditions:
If you want your model class to have relation with ASP.NET table you must model ASP.NET table as another entity. I'm not sure if you can use ASP.NET classes for that because for example
MembershipUser
doesn't have parameterless public constructor which is required for EF. So you will most probably need to create duplicate classes and their mappings and use these classes when referencing ASP.NET entities.