ASP.NET MVC 成员资格映射

发布于 2025-01-01 02:10:03 字数 726 浏览 3 评论 0原文

在数据库上执行 aspnet_regsql 后,我正在用 C# 开发 ASP.NET。我想跟踪游戏对象并将它们映射到用户...如果可能的话,我希望用户有一个游戏列表,但显然我不能将其放入当前的会员实现中,除非我编写自己的游戏列表试图避免。

public List<Game> Games { get; set; }

然而,在 MVC 中,我如何实际将其映射到另一个模型中(它将使用相同的数据库,只是该数据库中的另一个表)。

public class Games
{
    [key]
    public int GameId

    Properties/Details about the game here.... 

    //Foreign key to map to the specific user this Game belongs to
    [Required]
    public int UserId
}

这真的有效吗?后台的 MVC 是否确保 UserIdaspnet_User.UserId?我不想创建 MembershipProvider 接口的扩展,但如果上述方法不起作用,我想我可能必须这样做!

另外,我对利用个人资料不感兴趣;如您所见,我尝试映射到的数据将是该应用程序的核心。当配置文件数据还需要映射到其他对象时,尝试解析它实际上是不可能的!

I'm developing ASP.NET in C# after performing aspnet_regsql on my database. I want to track Game objects and map them to users... if possible I'd like a user have a list of Games, but obviously I can't put that in the current Membership implementation unless I write my own which I'm trying to avoid.

public List<Game> Games { get; set; }

In MVC however, how do I actually map this in another model (which would be using the same database, would just be another table within that database).

public class Games
{
    [key]
    public int GameId

    Properties/Details about the game here.... 

    //Foreign key to map to the specific user this Game belongs to
    [Required]
    public int UserId
}

Would this actually work? Does MVC in the background make sure that UserId is aspnet_User.UserId? I'd hate to create a extension of the MembershipProvider interface, but if the above doesn't work I suppose I might have to!

Also, I'm not interested in leveraging profiles; as you can see, the data I'm trying to map to will be central to this application. Trying to parse through profile data is practically impossible when it also needs to map to other objects!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不弃不离 2025-01-08 02:10:03

如果您需要使用 aspnet_User 加入Game,那么您的设置将不起作用。为此,您必须将 aspnet_User 映射为实体。

另一方面,如果您只需要检索特定用户的游戏,那么您的模型就可以工作。确保手动创建 FK 以保持数据完整性。

编辑

您可以将 aspnet_User 表映射为实体,如下所示

[Table("aspnet_User")]
public class User
{
     public int UserId { get; set; }
     // other props
}

If you need to join the Game with aspnet_User, then your setup will not work. For that you would have to map aspnet_User as an entity.

On the other hand if you only need to retrieve the Games for a particular user then your model will work. Make sure you manually create a FK to keep the data integrity.

Edit

You can map the aspnet_User table as an entity as follows

[Table("aspnet_User")]
public class User
{
     public int UserId { get; set; }
     // other props
}
脱离于你 2025-01-08 02:10:03

当你添加一个新游戏时,只需获取当前用户的 id,它是一个 guid(不是上面的 INT - 你需要一个 guid,它更安全,更不容易被猜到,以防它存储在表单上并允许用户向其中注入新值)
这部分是

Membership.GetUser().ProviderUserKey

要查询这将是简单的

ctx.Games.Where(o=>o.UserId=Membership.GetUser().ProviderUserKey) 

如果您想要那里的关系,请考虑@Eranga的关系。如果您想考虑级联删除(如删除用户,所有游戏都会再见),请参阅以下部分:< a href="http://msdn.microsoft.com/en-us/library/bb738695.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb738695.aspx

When you add a new game, simply get the current user's id which is a guid (not an INT as above - you want a guid, its a bit more secure and less easily guessable in case this is ever stored on a form and allows a user to inject new values into it)
That part is

Membership.GetUser().ProviderUserKey

To query this would be simply

ctx.Games.Where(o=>o.UserId=Membership.GetUser().ProviderUserKey) 

If you want the relation there though, consider @Eranga's relationship.If you want to consider cascade deletes (as in you delete the user, all games go bye bye) then see the section at: http://msdn.microsoft.com/en-us/library/bb738695.aspx

百合的盛世恋 2025-01-08 02:10:03

不,没有什么神奇的事情发生。您需要自己将正确的 ID 分配给 UserId

EF 或 MVC 如何知道您想要使用登录用户的 id?没有任何暗示任何空的 UserId 属性应该是当前用户的。

No. Nothing magical is going on. You need yourself to assign the correct id to UserId.

How could EF or MVC know that you want to use the logged in user's id? Nothing implies that any empty UserId property should be the current user's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文