ASP.NET MVC:模型和 MembershipUsers 之间的关系
我有一个模型,Docs,带有一个 guid 列,author_id,它应该引用 MembershipUser。
我正在使用标准的 MembershipProvider (我只更改了数据库名称),当然我没有 MembershipUsers 的模型。
我可以轻松地在 Docs 类中定义一些方法来获取 Doc 的 MembershipUser 和 MembershipUser 的所有 Docs,但有些事情很棘手(列出用户,显示每个用户拥有多少个文档); 此外,我觉得我以一种奇怪的方式使用 MVC:在我看来,如果我有一个 MembershipUsers 模型会更容易......
我如何实现这种关系? 我应该为 MembershipUsers 实现一个模型吗?
谢谢
更新:我意识到我根本不清楚。
假设我想列出我的所有用户及其文档。 一种方法是:
ViewModel model = new ViewModel()
{
Users = from MembershipUser user in Membership.GetAllUsers(page, pagesize, out totalusers)
select new UserWithDocs
{
User = user,
Docs = context.Docs.Where(c => c.author_id == (Guid) user.ProviderUserKey)
},
UsersCount = totalusers
};
这可行,但会为每个用户生成一个单独的查询。
我可以获取一组用户的 guid,然后查询文档 whereauthor_id IN list_of_guids
,但随后我应该手动将每个文档与其作者关联。
什么是更好的解决方案?
I have a model, Docs, with a guid column, author_id, which should reference a MembershipUser.
I'm using the standard MembershipProvider (I only changed the db name), and of course I haven't a model for MembershipUsers.
I can easily define some methods in class Docs to get MembershipUser for a Doc and all Docs for a MembershipUser, but some things are tricky (listing users, show how many docs each user has); furthermore, I feel I'm using MVC in an odd way: it seems to me that it would be easier if I had a MembershipUsers model...
How can I implement this relationship? Should I implement a model for MembershipUsers?
thanks
update: I realize I wasn't clear at all.
Say I want to list all my users and their docs.
One way to do this is:
ViewModel model = new ViewModel()
{
Users = from MembershipUser user in Membership.GetAllUsers(page, pagesize, out totalusers)
select new UserWithDocs
{
User = user,
Docs = context.Docs.Where(c => c.author_id == (Guid) user.ProviderUserKey)
},
UsersCount = totalusers
};
This works, but generates one separate query for each user.
I could get an array of users' guids and then query for Docs where author_id IN list_of_guids
, but then I should manually associate each doc to its author.
What is a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我是否理解正确,但您可以使用 MembershipUser 类作为用户模型。 如果您想将用户引用到您的“doc”模型,您可以简单地将属性“Author”添加到您的模型类中,当您从数据库获取模型时,您也可以获取用户。
Membership 类包含一些静态方法来列出所有用户、添加/删除用户等。
not sure if i understand you correctly, but you can use the MembershipUser class as your model for users. if you want to reference user to your "doc" model, you can simply add a property "Author" to you model class and when you fetch the model from DB you can get the user as well.
the Membership class contains some static methods to list all users, add/delete users etc.