简体   繁体   English

首先使用代码将ASP.NET MVC3将帖子链接到帐户模型

[英]ASP.NET MVC3 linking posts to the account models using code first

I have a newly created ASP.net MVC3 project with all the defaults. 我有一个新创建的带有所有默认设置的ASP.net MVC3项目。 I am using the approach to build the database. 我正在使用这种方法来建立数据库。 I come from a PHP background and this is my first attempt at a ASP.net project. 我来自PHP背景,这是我第一次尝试ASP.net项目。

I created a new model called "posts" and I want to link it to the built in accounts/users system that came with the default project. 我创建了一个名为“ posts”的新模型,我想将其链接到默认项目随附的内置帐户/用户系统。

Only signed in users can add posts, and I want to know what user added what post. 只有登录的用户可以添加帖子,我想知道是哪个用户添加了什么帖子。 I want to link the posts to a single user. 我想将帖子链接到单个用户。

In PHP I would create a FK in the posts table to the users table "posts_id" and when a user adds a new post I would fill in that field with the users's id. 在PHP中,我将在posts表中创建一个FK到users表“ posts_id”中,当用户添加新的post时,我将使用用户的id填写该字段。 But there does not seem to be a ID field in the account model. 但是帐户模型中似乎没有ID字段。

Questions 问题

  • How do I link a model to the default account system build in to ASP.net MVC3 using code first approach? 如何使用代码优先方法将模型链接到内置到ASP.net MVC3的默认帐户系统中?
  • How do I query for user fields from a view in the post controller? 如何在发布控制器中的视图中查询用户字段?

Links to a tutorials would work for me. 教程的链接对我有用。 Thanks 谢谢

FKs like posts_id is a database style reference. FK之类的posts_id是数据库样式参考。 The object oriented way of representing that data is by giving the Account object a ICollection<Post> and by giving each Post object an Account reference field. 表示该数据的面向对象的方式是,给Account对象一个ICollection<Post>并给每个Post对象一个Account引用字段。

Eg 例如

public class Post
{
    public Account Account { get; set; }
    ...
}

As for retrieving this data, I would suggest a repository-style service class which would be responsible for saving/loading objects to & from the DB. 至于检索这些数据,我建议使用一种存储库样式的服务类,该类负责将对象保存到数据库或从数据库中加载对象。 I am not sure exactly how the Account records get persisted in the template MVC app so I can't say how it would tie into that data. 我不确定帐户记录如何确切地保存在模板MVC应用程序中,因此我无法说出它如何与该数据绑定。 I have always used my own User DB table and thrown away all that build in Microsoft account stuff. 我一直使用自己的User DB表,并丢弃所有内置在Microsoft帐户中的东西。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM