简体   繁体   English

VS Community 2015 MVC模板-无法访问自定义模型

[英]VS Community 2015 MVC Template - Cannot access custom model

I'm currently learning Asp.NET MVC, I have started with the Ouf of the Box template from Vs2015 but I am having problems getting data from custom table 我目前正在学习Asp.NET MVC,我从Vs2015开始使用Box的Ouf模板,但是从自定义表获取数据时遇到了问题

Goal: My Goal is to add a contact list for the standard ApplicationUser using the following class: 目标:我的目标是使用以下类为标准ApplicationUser添加联系人列表:

public class UserContact
{        
    [Key]
    public virtual int Id { get; set; }

    public virtual ApplicationUser User { get; set; }
    public virtual ApplicationUser Contact { get; set; }
}

I have also added the following to my Application User: 我还向我的应用程序用户添加了以下内容:

public virtual ICollection<UserContact> ContactsList { get; set; }

And the following line to my ApplicationDbContext: 下面是我的ApplicationDbContext的行:

public DbSet<UserContact> UserContacts { get; set; }

The problem is that when I try to access the USerContacts table from my custome controller ContactController using 问题是,当我尝试使用以下方法从我的自定义控制器ContactController访问USerContacts表时

ApplicationDbContext db = new ApplicationDbContext();

var UserId = SomUserID  
db.UserContacts.Where(x => x.User == UserId)

The where clause does not get recognised at all. where子句根本无法识别。 I have useed the commands enable-migration 我已经使用了命令enable-migration

add-migration ContactsList add-migration联系人列表

update-database 更新数据库

Which ran with no errors and the table is now in the database. 哪个运行都没有错误,并且该表现在位于数据库中。 But I am unable to access the table. 但是我无法访问该表。 why? 为什么? what am I doing wrong? 我究竟做错了什么?

Please note that according to VS it is as if the .Where function is not available on the DbSet in my case 请注意,根据VS,就我而言,好像.Where函数在DbSet上不可用

And linq is imported as this is the standard when adding a new empty controller with Vs2015. 导入linq是因为这是在Vs2015中添加新的空控制器时的标准。

The Database was created during the update-database 该数据库是在更新数据库期间创建的

But ApplicationDbContext : IdentiyDbContext class does not let me access any other tables including Contact class, so, when I used in my controller 但是ApplicationDbContext:IdentiyDbContext类不允许我访问包括Contact类的任何其他表,因此,当我在控制器中使用时

"db.UserContacts.Where < x => x.User == someID >();" “ db.UserContacts.Where <x => x.User == someID>();”

it doesn recognise what x.User is from the UserContact class. 它不能从UserContact类中识别出x.User是什么。

EDIT: I found a stack overflow post where the user was missing a using statement: 编辑:我发现了一个堆栈溢出帖子,其中用户缺少using语句:

using System.Linq;

DbContext -> DbSet -> Where clause is missing (Entity Framework 6) DbContext-> DbSet-> Where子句缺失(实体框架6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM