我很难从EF中检索正确相关的数据。这是我第一次使用代码生成数据,所以这里是我的课程(为了简洁而被剥离)
Author.cs
public class Author
{
[Key]
public int author_id {get;set;}
public string author_name {get;set;}
public string author_email {get;set;}
public virtual ICollection<AuthorCategory> AuthorCategories {get; set;}
public Author()
{
this.AuthorCategories = new HashSet<AuthorCategory>();
}
}
AuthorCategory.cs
public class AuthorCategory
{
[Key]
public int category_id { get; set; }
public string category_name { get; set; }
public virtual ICollection<Author> Authors { get; set; }
public AuthorCategory()
{
this.Authors = new HashSet<Author>();
}
}
AuthorRelation.cs
public class AuthorCategoryRelation
{
public AuthorCategoryRelation()
{
this.AuthorCategories = new HashSet<AuthorCategory>();
this.Authors = new HashSet<Author>();
}
public int author_id { get; set; }
public int category_id { get; set; }
public ICollection<AuthorCategory> AuthorCategories { get; set; }
public ICollection<Author> Authors { get; set; }
}
我希望检索每个作者的列表以输出作者姓名,电子邮件和每个作者所属的(可能的多个)类别,我跑;
var authors = (from a in db.Authors
select new
{
first_name = a.first_name,
last_name = a.last_name,
cats = a.AuthorCategories.ToList()
});
我以JSON格式返回结果集。所以我期望的是一个简单的输出结果就是这样的
{
"Authors": [
{
"AuthorCategories": [
{
"Authors": [
{
"AuthorCategories": [],
"author_id": 8,
"first_name": "Ava",
"last_name": "Mcmillan",
"photo_path": "C:\\Inetpub\\HwHosts\\ontayka.com\\img\\authors\\t1_ava_mcmillan.jpg",
"photo_social_sharing": null,
"username": "xoxywaf",
"password": "8810160BFD79D7DE46388533E26FFD247D152514",
"pass_guid": "7b574629-32b0-4905-b51c-4315a446e96e",
"email": "kekozod@yahoo.com",
"twitter": "Ut aspernatur blanditiis reprehenderit deserunt aut autem accusantium ea natus irure rerum quaerat",
"facebook": "Voluptatem Deserunt illo in sit",
"date_created": "2016-08-16T17:31:50.383",
"date_last_login": null,
"author_status": 0,
"about_author": "Qui quis laboriosam, pariatur? In in est, sunt optio, sit, quia et.",
"require_publish_approval": true,
"column_name": null,
"column_image": null,
"change_password_at_first_login": false,
"tenant_id": 1,
"Posts": null
}
],
"category_id": 2,
"category_name": "Ekonomi",
"tenant_id": 0
}
],
"author_id": 7,
"first_name": "Caryn",
"last_name": "Hines",
"photo_path": "C:\\Inetpub\\HwHosts\\ontayka.com\\img\\authors\\t1_caryn_hines.jpg",
"photo_social_sharing": null,
"username": "jocusadom",
"password": "609DE1F0C08B8FC4A797E381A160A64285A3F1D8",
"pass_guid": "324bf75b-f22e-486f-905d-45623f34a09b",
"email": "favojyj@hotmail.com",
"twitter": "Sed aliquid dolores veniam explicabo In proident rem sit nisi fugiat laboris nisi esse aut",
"facebook": "Magnam dolorum voluptatem atque voluptate assumenda velit rerum ducimus itaque eos aut",
"date_created": "2016-08-16T17:30:46.44",
"date_last_login": null,
"author_status": 1,
"about_author": "Deserunt et consequatur, fuga. Dolores et fugit, exercitationem alias fugiat iusto adipisicing nemo ullam eaque harum nemo sit, dignissimos.",
"require_publish_approval": false,
"column_name": null,
"column_image": null,
"change_password_at_first_login": false,
"tenant_id": 1,
"Posts": null
}
],
"category_id": 1,
"category_name": "Spor",
"tenant_id": 0
}
显然这里肯定是错的。我非常感谢任何有经验的开发人员来指导我。我正在定义分类错误?或者是我做错了什么?
提前致谢!