从表中获取多个数据使用Entity Framework数据模型

时间:2014-12-27 10:53:03

标签: c# sql asp.net entity-framework

我正在使用Entity Framework模型数据来处理来自数据库的数据(CRUD操作)。我想从表中获取所有数据(不只是一个)。

这是数据库模型:

Database model

我想从所有表中获取多个数据。

目前我正在使用显示的查询,但此查询的问题是我从Contact表中获取了多个值,而其他表只显示了一个结果。有人知道为什么我的查询不起作用以及如何从表中获取所有多个数据。

这是从数据库获取所有数据的查询/函数:

ContactsEntities db = new ContactsEntities();
        //get all contacts
        public JsonResult GetAll()
        {
            var data = (from c in db.Contacts
                        from e in db.Emails.Where(x => x.id == c.id).DefaultIfEmpty()
                        from p in db.Phones.Where(x => x.id == c.id).DefaultIfEmpty()
                        from t in db.Tags.Where(x => x.id == c.id).DefaultIfEmpty()
                        select new
                        {
                            id = c.id,
                            phones = p.number,
                            emails = e.email1,
                            tags = t.tag1,
                            firstname = c.firstname,
                            lastname = c.lastname,
                            address = c.address,
                            city = c.city,
                            bookmarked = c.bookmarked,
                            notes = c.notes
                        }).ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        } 

3 个答案:

答案 0 :(得分:10)

我已经在您的模型上测试了它:

var test1 = (from c in db.Contacts
                   join e in db.Emails
                       on c.id equals e.id_contact
                   join t in db.Tags
                   on c.id equals t.id_contact
                   join p in db.Phones on c.id equals p.id_contact
                   select new
                   {
                       id = c.id,
                       phones = p.number,
                       emails = e.email1,
                       tags = t.tag1,
                       firstname = c.firstname,
                       lastname = c.lastname,
                       address = c.address,
                       city = c.city,
                       bookmarked = c.bookmarked,
                       notes = c.notes
                   }).ToList();

我试图在一步中解决这个问题,否则在test1之后添加它可以正常工作:

 var result = (from contact in test1
                    group contact by contact.id into grp
                    select new
                    {
                        id = grp.Key,
                        firstname = grp.First().firstname,
                        lastname = grp.First().lastname,
                        address = grp.First().address,
                        city = grp.First().city,
                        bookmarked = grp.First().bookmarked,
                        notes = grp.First().notes,
                        phones = grp.Where(x => x.phones != null).Select(x => x.phones).Distinct().ToArray(),
                        emails = grp.Where(x => x.emails != null).Select(x => x.emails).Distinct().ToArray(),
                        tags = grp.Where(x => x.tags != null).Select(x => x.tags).Distinct().ToArray()
                    }).ToList();

如果您建立了它们之间的关系,它将被解析,此代码将根据您的需要返回所有联系人:

1-创建新图

2-添加这些表格,然后将联系人的ID拖到' id_contact'每个电子邮件,标签和电话

3-在Sql Server上保存图表

4-在Visual Studio中重新创建模型

Relation

var contacts = (from c in db.Contacts
                       select c).ToList();

对于每个联系人,它只会通过关系获得所有相关的电子邮件,电话和标签。

答案 1 :(得分:1)

您将x.id链接到c.id.我认为你需要将x.id_contact链接到c.id. (电话和标签的问题相同)

        var data = (from c in db.Contacts
                    from e in db.Emails.Where(x => x.id_contact == c.id).DefaultIfEmpty()
                    from p in db.Phones.Where(x => x.id_contact == c.id).DefaultIfEmpty()
                    from t in db.Tags.Where(x => x.id_contact == c.id).DefaultIfEmpty()
                    select new
                    {
                        id = c.id,
                        phones = p.number,
                        emails = e.email1,
                        tags = t.tag1,
                        firstname = c.firstname,
                        lastname = c.lastname,
                        address = c.address,
                        city = c.city,
                        bookmarked = c.bookmarked,
                        notes = c.notes
                    }).ToList();
        return Json(data, JsonRequestBehavior.AllowGet);

根据电话',电子邮件等来判断?和'标签' select的属性,我认为你希望每个联系人有1条记录。您可以通过以下方式实现此目的:

       var data = (from c in db.Contacts
                from e in db.Emails.Where(x => x.id_contact == c.id).DefaultIfEmpty()
                from p in db.Phones.Where(x => x.id_contact == c.id).DefaultIfEmpty()
                from t in db.Tags.Where(x => x.id_contact == c.id).DefaultIfEmpty()
                select new
                {
                    id = c.id,
                    phone = (p != null ? p.number : null),
                    email = (e != null ? e.email1 : null),
                    tag = (t != null ? t.tag1 : null),
                    firstname = c.firstname,
                    lastname = c.lastname,
                    address = c.address,
                    city = c.city,
                    bookmarked = c.bookmarked,
                    notes = c.notes
                }).ToList();

        var data2 = (from i in data
                    group i by i.id into grp
                    select new
                    {
                        id = grp.Key,
                        phones = grp.Where(x => x.phone != null).Select(x => x.phone).ToArray(),
                        emails = grp.Where(x => x.email != null).Select(x => x.email).ToArray(),
                        tags = grp.Where(x => x.tag != null).Select(x => x.tag).ToArray(),
                        firstname = grp.First().firstname,
                        lastname = grp.First().lastname,
                        address = grp.First().address,
                        city = grp.First().city,
                        bookmarked = grp.First().bookmarked,
                        notes = grp.First().notes
                    }).ToList();

答案 2 :(得分:0)

如果外部键是所有其他表中的联系人,则以下代码将在C#-EntityFramework6-MVC5中起作用:

var conts= db.Contacts;
db.Entry(conts).Collection(c => c.Emails).Load();
// If you retrieve an email, use "Reference" instead of "Collection"

实体框架文档:

using (var context = new BloggingContext())
{
   var post = context.Posts.Find(2);

   // Load the blog related to a given post
   context.Entry(post).Reference(p => p.Blog).Load();

   // Load the blog related to a given post using a string
   context.Entry(post).Reference("Blog").Load();

   var blog = context.Blogs.Find(1);

   // Load the posts related to a given blog
   context.Entry(blog).Collection(p => p.Posts).Load();

   // Load the posts related to a given blog
   // using a string to specify the relationship
   context.Entry(blog).Collection("Posts").Load();
}