我不认为我的问题的标题是非常准确的,所以我会试着解释我的问题是什么。
我有2种型号:文章和用户实体框架自动化连接
public class Article
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string Content { get; set; }
[Required]
public User Author { get; set; }
public int Likes { get; set; }
}
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public ICollection<Article> MyArticles { get; set; }
}
和我的数据库上下文
public class Context: DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Article> Articles { get; set; }
}
有没有办法列出我从用户模型查看和访问数据的文章(表) ? 例子:如果我通过var mode = db.Articles
通过控制器 我可以做类似
foreach(var item in Model)
{
<p>item.Title</p>
<p>item.Content</p>
<p>item.Author.Name</p>
}
我敢肯定轨有这种可能性,我想EF 4.1了这一点。
我试过了,它给我这个错误_CS1061:'System.Collections.Generic.IEnumerable'没有包含'MyArticles'的定义,也没有包含接受'System.Collections.Generic'类型的第一个参数的扩展方法'MyArticles'。 IEnumerable '可以找到_ –
lesce
这是因为您需要将单个用户传入视图。否则,您需要遍历模型中的用户,并更改我的代码以便从外部循环中使用该用户。 – flipchart