【问题标题】:LINQ to SQL in ASP.NET MVC and Repository patternASP.NET MVC 和存储库模式中的 LINQ to SQL
【发布时间】:2011-09-30 15:51:11
【问题描述】:

我正在尝试学习使用 LINQ to Entities 的 Asp.NET MVC 网站上的教程,但我决定改用 LINQ to SQL。我正在创建一个名为 Groups 的新表,该表与 Contacts 表有关系。本质上,它是一对多关系,其中一个组可以有多个联系人,而一个联系人只能有 1 个组。请参阅下面带有 CRUD 操作的示例代码。

我不确定如何在 LINQ to SQL 中实现这一点。例如,您如何在 LINQ to SQL 中执行此操作:

return _entities.GroupSet.Include("Contacts").FirstOrDefault();

你应该为这两个表做一个 JOIN 还是有其他方法?

示例代码:

using System.Collections.Generic;
using System.Linq;
using System;

namespace ContactManager.Models
{
    public class EntityContactManagerRepository : ContactManager.Models.IContactManagerRepository
    {
        private ContactManagerDBEntities _entities = new ContactManagerDBEntities();

        // Contact methods

        public Contact GetContact(int id)
        {
            return (from c in _entities.ContactSet.Include("Group")
                    where c.Id == id
                    select c).FirstOrDefault();
        }

        public Contact CreateContact(int groupId, Contact contactToCreate)
        {
            // Associate group with contact
            contactToCreate.Group = GetGroup(groupId);

            // Save new contact
            _entities.AddToContactSet(contactToCreate);
            _entities.SaveChanges();
            return contactToCreate;
        }

        public Contact EditContact(int groupId, Contact contactToEdit)
        {
            // Get original contact
            var originalContact = GetContact(contactToEdit.Id);

            // Update with new group
            originalContact.Group = GetGroup(groupId);

            // Save changes
            _entities.ApplyPropertyChanges(originalContact.EntityKey.EntitySetName, contactToEdit);
            _entities.SaveChanges();
            return contactToEdit;
        }

        public void DeleteContact(Contact contactToDelete)
        {
            var originalContact = GetContact(contactToDelete.Id);
            _entities.DeleteObject(originalContact);
            _entities.SaveChanges();
        }

        public Group CreateGroup(Group groupToCreate)
        {
            _entities.AddToGroupSet(groupToCreate);
            _entities.SaveChanges();
            return groupToCreate;
        }

        // Group Methods

        public IEnumerable<Group> ListGroups()
        {
            return _entities.GroupSet.ToList();
        }

        public Group GetFirstGroup()
        {
            return _entities.GroupSet.Include("Contacts").FirstOrDefault();
        }

        public Group GetGroup(int id)
        {
            return (from g in _entities.GroupSet.Include("Contacts")
                       where g.Id == id
                       select g).FirstOrDefault();
        }

        public void DeleteGroup(Group groupToDelete)
        {
            var originalGroup = GetGroup(groupToDelete.Id);
            _entities.DeleteObject(originalGroup);
            _entities.SaveChanges();

        }

    }
}

【问题讨论】:

  • 迈克你为什么不使用实体框架?在 .NET 4 和 4.5 DP 中,微软有点不认可 Linq to Sql 那么为什么要使用它呢?如果你正在开始一个新项目,我强烈建议使用 EF
  • Argh...我希望使用 Linq to SQL,因为我更熟悉它。我已经在尝试学习 MVC,但我不想同时学习 EF。我想我要更改我的解决方案以使用 EF。感谢您的帮助。

标签: asp.net-mvc linq-to-sql


【解决方案1】:

您需要指定一些DataLoadOptions 来为您创建连接:

为此,您必须使用正确的DataLoadOptions 为每种类型的查询创建一个DataContext:

var db = new WhateverDbDataContext();
DataLoadOptions options = new DataLoadOptions();
db.LoadOptions = options;
options.LoadWith(x => x.Contacts);

return db.SomeTable.FirstorDefault();

【讨论】:

    【解决方案2】:

    Linq to sql 不支持 Include 方法。如果您不关心关系是否延迟加载,那么您无需执行任何操作。如果您希望它被预先加载,那么您可以使用更复杂的 DataLoadOptions。

    见这篇文章:

    http://blog.stevensanderson.com/2007/12/02/linq-to-sql-lazy-and-eager-loading-hiccups/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2012-02-21
      相关资源
      最近更新 更多