Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在Asp.Net中实现多对多关系(困惑?)_Asp.net Mvc_Database_Entity Framework_Many To Many - Fatal编程技术网

Asp.net mvc 在Asp.Net中实现多对多关系(困惑?)

Asp.net mvc 在Asp.Net中实现多对多关系(困惑?),asp.net-mvc,database,entity-framework,many-to-many,Asp.net Mvc,Database,Entity Framework,Many To Many,我目前的目标是使用实体框架和ASP MVC中的类构建一个数据库结构 我目前有一个Users表和一个Posts表。我想做的是为喜欢帖子的用户创建多对多关系(同时保留创建帖子的用户)。并且能够为每个用户访问他们喜欢的所有帖子。目前我有这些类,但我不确定如何链接它们,因为所有在线示例都链接来自不同数据库的主键,我只想使用Username参数。任何帮助都会很好。到目前为止,我有这个 public class Posts { [Key] public virtua

我目前的目标是使用实体框架和ASP MVC中的类构建一个数据库结构

我目前有一个Users表和一个Posts表。我想做的是为喜欢帖子的用户创建多对多关系(同时保留创建帖子的用户)。并且能够为每个用户访问他们喜欢的所有帖子。目前我有这些类,但我不确定如何链接它们,因为所有在线示例都链接来自不同数据库的主键,我只想使用Username参数。任何帮助都会很好。到目前为止,我有这个

 public class Posts
    {
        [Key]
        public virtual int PostId { get; set; }
        public virtual string Title { get; set; }
        public virtual string URL { get; set; }
        [DisplayName("Main Text")]
        public virtual string TextBody { get; set; }
        public int PostLikes { get; set; }
        private DateTime Datedata = DateTime.Now;
        public DateTime PostDate { get { return Datedata; } set { Datedata = value; } }
        public virtual Users User { get; set; }
        public ICollection<PostLikes> UsersWhoHaveSigned { get; set; }
    }
{
    public class Users
    {
        [Key]
        public virtual int UserId { get; set; }
        public virtual string Username { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual List<Posts> Post { get; set; }

    }
}
公共类职位
{
[关键]
公共虚拟int PostId{get;set;}
公共虚拟字符串标题{get;set;}
公共虚拟字符串URL{get;set;}
[显示名称(“主文本”)]
公共虚拟字符串TextBody{get;set;}
公共int PostLikes{get;set;}
private DateTime Datedata=DateTime.Now;
public DateTime PostDate{get{return Datedata;}set{Datedata=value;}}
公共虚拟用户用户{get;set;}
公共ICollection用户已签名{get;set;}
}
{
公共类用户
{
[关键]
公共虚拟int用户标识{get;set;}
公共虚拟字符串用户名{get;set;}
公共虚拟字符串FirstName{get;set;}
公共虚拟字符串LastName{get;set;}
公共虚拟列表Post{get;set;}
}
}
我还没有构建已签名的用户表。早期的实验让我很痛苦。任何帮助都会很好

编辑:我希望寻求帮助,然后适当的信息,以适合我的例子,利用个人帐户插件。这会产生一些附加文件,这些文件现在会干扰您提供的代码。 这是IdentityModels.cs文件

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace Coursework2.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    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 class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}
使用System.Data.Entity;
使用System.Security.Claims;
使用System.Threading.Tasks;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
名称空间Coursework2.Models
{
//您可以通过向ApplicationUser类添加更多属性来为用户添加配置文件数据,请访问https://go.microsoft.com/fwlink/?LinkID=317594 了解更多。
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
}
}
我相信顶部的汇编指令阻止使用system.data.entity,因此当我尝试实现ApplicationDbContext:DbContext时,会收到错误消息:/


理想情况下,我希望使用IdentityModels.cs文件替换users类。但是仍然很失落。

首先,我建议您对类名使用单数形式,因为EF会自动将表名复数化

其次,对于键属性,您可以只使用术语Id,而不使用任何注释,EF将把它作为主键

最后,我假设您希望使用代码优先的方法。考虑下面的类(你的,但是为了清晰的目的而重构):

公共类职位
{
公共虚拟Guid Id{get;set;}
公共虚拟字符串用户名{get;set;}
公共虚拟用户用户{get;set;}
公共虚拟ICollection类似于{get;set;}
}
公共类PostLike
{
公共虚拟Guid Id{get;set;}
公共虚拟Guid PostId{get;set;}
公共虚拟Post{get;set;}
公共虚拟字符串用户名{get;set;}
公共虚拟用户用户{get;set;}
}
公共类用户
{
公共虚拟Guid Id{get;set;}
公共虚拟字符串用户名{get;set;}
公共虚拟ICollection Posts{get;set;}
公共虚拟ICollection类似于{get;set;}
}
要使其工作,您需要一个DbContext,如下所示。请注意OnModelCreating方法,这就是神奇发生的地方:

public class ApplicationDbContext
    : DbContext
{

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
        : base(options)
    {

    }

    public DbSet<Post> Posts { get; set; }

    public DbSet<PostLike> PostLikes { get; set; }

    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<User>()
            .HasAlternateKey(u => u.UserName);
        modelBuilder.Entity<User>()
            .HasMany(u => u.Posts)
            .WithOne(p => p.User);
        modelBuilder.Entity<Post>()
            .HasOne(p => p.User)
            .WithMany(u => u.Posts)
            .HasForeignKey(p => p.UserName)
            .HasPrincipalKey(u => u.UserName);
        modelBuilder.Entity<Post>()
            .HasMany(p => p.Likes)
            .WithOne(pl => pl.Post);
        modelBuilder.Entity<PostLike>()
            .HasOne(pl => pl.Post)
            .WithMany(p => p.Likes);
        modelBuilder.Entity<PostLike>()
            .HasOne(pl => pl.User)
            .WithMany(u => u.Likes)
            .HasForeignKey(pl => pl.UserName)
            .HasPrincipalKey(u => u.UserName);
    }

}
public类ApplicationDbContext
:DbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
公共DbSet Posts{get;set;}
公共DbSet PostLikes{get;set;}
公共数据库集用户{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity()
.HasAlternateKey(u=>u.UserName);
modelBuilder.Entity()
.HasMany(u=>u.Posts)
.有一个(p=>p.User);
modelBuilder.Entity()
.HasOne(p=>p.User)
.有许多(u=>u.Posts)
.HasForeignKey(p=>p.UserName)
.HasPrincipalKey(u=>u.UserName);
modelBuilder.Entity()
.HasMany(p=>p.Likes)
.有一个(pl=>pl.Post);
modelBuilder.Entity()
.HasOne(pl=>pl.Post)
.有很多(p=>p.like);
modelBuilder.Entity()
.HasOne(pl=>pl.User)
.有很多(u=>u.like)
.HasForeignKey(pl=>pl.UserName)
.HasPrincipalKey(u=>u.UserName);
}
}
瞧!我希望它能回答你的问题;) 如果是这样,请别忘了把我的帖子标记为答案

public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Post> Posts { get; set; } public DbSet<PostLike> PostLikes { get; set; } public DbSet<User> Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<User>() .HasAlternateKey(u => u.UserName); modelBuilder.Entity<User>() .HasMany(u => u.Posts) .WithOne(p => p.User); modelBuilder.Entity<Post>() .HasOne(p => p.User) .WithMany(u => u.Posts) .HasForeignKey(p => p.UserName) .HasPrincipalKey(u => u.UserName); modelBuilder.Entity<Post>() .HasMany(p => p.Likes) .WithOne(pl => pl.Post); modelBuilder.Entity<PostLike>() .HasOne(pl => pl.Post) .WithMany(p => p.Likes); modelBuilder.Entity<PostLike>() .HasOne(pl => pl.User) .WithMany(u => u.Likes) .HasForeignKey(pl => pl.UserName) .HasPrincipalKey(u => u.UserName); } }