【问题标题】:Many to many with extra foreign key?多对多有额外的外键?
【发布时间】: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


【解决方案1】:

用户表

public partial class User
{

 public User()
 {
     this.Posts = new HashSet<Post>();
     this.UserPosts = new HashSet<UserPost>();
 }

 public int UserId { get; set; }
 public string Username { get; set; }

 public virtual ICollection<Post> Posts { get; set; }

 public virtual ICollection<UserPost> UserPosts { get; set; }
}

发布表格

public partial class Post
{
 public Post()
 {
     this.UserPosts = new HashSet<UserPost>();
 }

 public int PostId { get; set; }
 public string Message { get; set; }
 public Nullable<System.DateTime> Date { get; set; }
 public Nullable<int> UserId { get; set; }

 public virtual User User { get; set; }

 public virtual ICollection<UserPost> UserPosts { get; set; }
}

还有你的映射表,像这样 你的列 1) Id (pk), 2) UserId (fk) 3) PostId (fk)

使用实体框架表需要一个主键。

UserPost 表

public partial class UserPost
{
 public int Id { get; set; }
 public Nullable<int> UserId { get; set; }
 public Nullable<int> PostId { get; set; }

 public virtual Post Post { get; set; }
 public virtual User User { get; set; }
}

更新代码

modelBuilder.Entity<Post>().ToTable("userid table name");

这一行添加到这个类的下面方法ApplicationDbContext

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{


}

【讨论】:

  • 是否必须明确设置用户 UserPost 才能在帖子中获取外键?如果您查看我的代码,它会生成一个 userPost 表和多对多关系,并且工作正常。问题是在 post 表中向 asp.net 身份用户添加 FK 密钥。可以看到您在 post 类中使用了类似的代码“public virtual User User { get; set; }”,但它对我不起作用。
  • 您的代码有效,但它不是我正在寻找的。您创建了一个新类用户,但我想使用 Applicationuser。当您使用 asp.net Identity 创建一个新项目时,会自动创建一个用户表 (AspNetUser),我想使用该已生成的表。见我的第一篇文章。问题是在 post 表中获取该 UserId。像 public string Id { get;放; } 不起作用。
  • 是的,我明白了。让我试试你的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 2014-07-13
  • 2018-02-19
  • 1970-01-01
相关资源
最近更新 更多