Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework asp.net内核中的多对多关系和多选_Entity Framework_Razor_Asp.net Core - Fatal编程技术网

Entity framework asp.net内核中的多对多关系和多选

Entity framework asp.net内核中的多对多关系和多选,entity-framework,razor,asp.net-core,Entity Framework,Razor,Asp.net Core,我正在尝试用razor学习asp.net内核,我正在尝试制作一个视频游戏数据库,以记录已完成的游戏、我尚未玩过的游戏等 但我有个问题。我有一张桌子游戏和一张桌子开发者。因为一个游戏可以有很多开发者,一个开发者可以有很多游戏,所以y制作了第三张表DeveloperXGame。 他们是这样的 public class Game { public int Id { get; set; } public string Name { get; set; } } public class

我正在尝试用razor学习asp.net内核,我正在尝试制作一个视频游戏数据库,以记录已完成的游戏、我尚未玩过的游戏等

但我有个问题。我有一张桌子
游戏
和一张桌子
开发者
。因为一个游戏可以有很多开发者,一个开发者可以有很多游戏,所以y制作了第三张表
DeveloperXGame
。 他们是这样的

public class Game
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Developer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class DeveloperXGame
{
    public int DeveloperId { get; set; }
    public int JuegoId { get; set; }
    public Developer Developer { get; set; }
    public Game Game { get; set; }
}

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

    }

    public DbSet<Game> Game { get; set; }
    public DbSet<Developer> Developer { get; set; }
    public DbSet<DeveloperXGame> DeveloperXGame { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DeveloperXGame>()
            .HasKey(m => new { m.DeveloperId, m.GameId });
    }
}
公共类游戏
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类开发人员
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类DeveloperXGame
{
public int DeveloperId{get;set;}
public int JuegoId{get;set;}
公共开发人员开发人员{get;set;}
公共游戏{get;set;}
}
公共类ApplicationDbContext:DbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
公共DbSet游戏{get;set;}
公共数据库集开发人员{get;set;}
公共DbSet DeveloperXGame{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasKey(m=>new{m.DeveloperId,m.GameId});
}
}
我已经为开发人员创建了这些页面,所以我首先手动创建它们。现在我正在尝试创建游戏,我想显示一个select,在这里我可以选择该列表中的一个或多个开发人员(下一步将尝试通过游戏页面添加它们,如果它们不存在)。但我已经迷失了这一点

我不知道如何加载该列表中的开发人员列表,以及稍后如何在DeveloperXGame表中保存这些选定的项目


谢谢

您可以从上下文中删除公共DbSet DeveloperXGame{get;set;}

Index.cshtml.cs

public class IndexModel : PageModel
{
    private readonly ApplicationDbContext _context;

    public IndexModel(ApplicationDbContext context)
    {
        _context = context;
    }

    public Game Game { get; set; }
    public IEnumerable<int> Developers { get; set; }
    public IEnumerable<SelectListItem> DeveloperList { get; set; }
    public IActionResult OnGet()
    {
        var developers = from m in _context.Developers
                     select m;
        DeveloperList = developers.Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Name });
        return Page();
    }
}


您还可以使用ViewData在视图中传递开发人员列表。你可以在官方文件中浏览这个示例网站。希望它能帮助你开始

谢谢!它起作用了。但是为什么我应该从上下文中删除公共DbSet DeveloperXGame{get;set;}?当我浏览列表将项目添加到数据库时,我不需要在OnPost方法中使用它吗?您可以保留它,但这并不重要。您可以通过模型访问它们,因为这是一种多对多关系<代码>公共列表DeveloperGames{get;set;}在您的模型中使用它!
@page
@model RazorPages.TestGame.Pages.Games.IndexModel
@{
   ViewData["Title"] = "Index";
}

<div class="row">
<div class="col-md-4">
    <form method="post">
        <div class="form-group">
            <label asp-for="Game.Name" class="control-label"></label>
            <input asp-for="Game.Name" class="form-control" />
        </div>
        <div class="form-group">
            <label asp-for="Developers" class="control-label"></label>
            <select asp-for="Developers" asp-items="Model.DeveloperList">
                <option value="">All</option>
            </select>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </form>
</div>