【发布时间】:2016-10-10 17:46:37
【问题描述】:
我想在 user 和 post 之间生成一个联结表,并且我希望在 post 表中包含 userId。
我有这个代码
public class Post
{
public Post()
{
this.ApplicationUser = new HashSet<ApplicationUser>();
}
public int PostId { get; set; }
public string Message { get; set; }
public DateTime MessageDate { get; set; }
public virtual ApplicationUser User { get; set; } //this is the problem
public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }
}
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public ApplicationUser()
{
this.Posts = new HashSet<Post>();
}
public virtual ICollection<Post> Posts { get; set; }
}
我得到了额外的联结表以及用户和帖子之间的多对多关系。但这是错误的。
public virtual ApplicationUser User { get; set; }
这会在 post 表中生成两个 UserId(applicationUser_id 和 User_id),在 User 表中生成 Post_PostId。我只想要 Post 表中的一个额外字段 FK UserId。
我想要三个这样的表
发帖
PostId 信息 日期 UserId FK
用户
用户 ID 以及 asp.net 身份用户中的其余字段
用户帖子
用户 ID 邮政编号
【问题讨论】:
-
检查我更新的代码
标签: asp.net asp.net-mvc-4 many-to-many code-first