ASP.Net MVC如何在ApplicationUser和我自己的类之间创建一对多的关系? [英] ASP.Net MVC How to create a one-to-many relationship between the ApplicationUser and my own class?

查看:178
本文介绍了ASP.Net MVC如何在ApplicationUser和我自己的类之间创建一对多的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio社区2015中创建一个ASP.Net MVC项目,我必须让用户创建和管理广告。这是一个一对多关系,意味着用户可以创建尽可能多的广告。每个用户都可以看到所有广告,但只有创建它们的广告才能编辑它们。由于MVC模板已经附带了Identity实现,我想使用ApplicationUser类作为我的用户类。

I am creating a ASP.Net MVC project in Visual Studio Community 2015 where I have to enable users to create and manage advertisements. This is a one-to-many relationship meaning that a user may create as many advertisements as he pleases. Every user can see all advertisements but only the ones that created them can edit them. Since the MVC template already comes with Identity implemented, I want to use the class ApplicationUser as my user class.

我将类型Avertisement添加到我的模型文件夹中,如下所示:

I added a class Avertisement to my Model folder that looks like this:

 public class Advertisement
{
    public int AdvertisementId { get; set; }
    public string Description { get; set; }

    public string ApplicationUserID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

然后我将Advertisement DbSet添加到ApplicationDbContext类中:

Then I added the Advertisement DbSet to the ApplicationDbContext class:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) {}

    public DbSet<Advertisement> Advertisements { get; set; }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

之后,我启用迁移并重建解决方案: / p>

Afterwards, I enable migrations and rebuild the solution:

Enable-Migrations
add-migrations InitialCreate
update-database

直到这一点,一切顺利。现在来我的问题我现在通过使用MVC 5控制器与视图,使用实体框架模板添加一个广告控制器,但生成的控制器附带以下错误,每当db.ApplicationUsers出现在控制器上时出现。我不知道我做错了什么,我在Stackoverflow和互联网上看到无处不在,但没有一个解决方案适用于我。你认为可能是错的?

Until this point everything runs smoothly. Now comes my problem. I now try scaffolding by adding an Advertisement controller with the "MVC 5 Controller with views, using Entity Framework" template but the generated controller comes with the following error that appears everytime db.ApplicationUsers appears on the controller. I have no idea what I am doing wrong and I've looked everywhere on stackoverflow and the internet but none of the solutions worked for me. What do you think could be wrong?

推荐答案

首先,您需要添加 ApplicationUser 广告,找到 ApplicationUser 类,然后添加:

First you need to add the link between ApplicationUser and Advertisment, find the ApplicationUser class and add this:

public class ApplicationUser : IdentityUser
{
    // Add this line
    // vvvvvvvvvvvvv
    public virtual ICollection<Advertisment> Advertisments { get; set; }
}

在您的操作方法中,您可以使用以下方式访问用户:

In your action method, you access the users with this:

db.Users

这篇关于ASP.Net MVC如何在ApplicationUser和我自己的类之间创建一对多的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆