2009-08-19 70 views
0

我的工作我的方式,通过Apress的“临ASP.NET MVC框架”(http://www.apress.com/book/view/9781430210078)一书,并在例如笔者使用创建的数据库表的链接(以及假资料库) LINQ是这样的: -LINQ的映射到多个表

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data.Linq.Mapping; 

namespace DomainModel.Entities 
{ 
    [Table(Name = "Products")] 
    public class Product 
    { 
     [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] 
     public int ProductID { get; set; } 

     [Column] public string Name { get; set; } 
     [Column] public string Description { get; set; } 
     [Column] public decimal Price { get; set; } 
     [Column] public string Category { get; set; } 
     public string this[string propName] 
     { 
      get { 
      if ((propName == "Name") && string.IsNullOrEmpty(Name)) 
       return "Please enter a product name"; 
      if ((propName == "Description") && string.IsNullOrEmpty(Description)) 
       return "Please enter a description"; 
      if ((propName == "Price") && (Price < 0)) 
       return "Price must not be negative"; 
      if ((propName == "Category") && string.IsNullOrEmpty(Category)) 
       return "Please specify a category"; 
      return null; 
     } 
    } 
    public string Error { get { return null; } } // Not required } 
} 

创建接口: -

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using DomainModel.Entities; 

namespace DomainModel.Abstract 
{ 
    public interface IProductsRepository 
    { 
     IQueryable Products { get; } 
     void SaveProduct(Product product); 
     void DeleteProduct(Product product); 
    } 
} 

假仓库(不含税),然后一个真正的数据库连接库: -

using System.ComponentModel; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using DomainModel.Abstract; 
using System.Data.Linq; 
using DomainModel.Entities; 

namespace DomainModel.Concrete 
{ 
    public class SqlProductsRepository : IProductsRepository 
    { 
     private Table productsTable; 
     public SqlProductsRepository(string connectionString) 
     { 
      productsTable = (new DataContext(connectionString)).GetTable(); 
     } 

     public IQueryable Products 
     { 
      get { return productsTable; } 
     } 

     public void SaveProduct(Product product) 
     { 
      EnsureValid(product, "Name", "Description", "Category", "Price"); 

      // If it's a new product, just attach it to the DataContext 
      if (product.ProductID == 0) 
       productsTable.InsertOnSubmit(product); 
      else { 
       // If we're updating an existing product, tell the DataContext 
       // to be responsible for saving this instance 
       productsTable.Attach(product); 
       // Also tell the DataContext to detect any changes since the last save 
       productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product); 
      } 

      productsTable.Context.SubmitChanges(); 
     } 

     public void DeleteProduct(Product product) 
     { 
      productsTable.DeleteOnSubmit(product); 
      productsTable.Context.SubmitChanges(); 
     } 

     private void EnsureValid(IDataErrorInfo validatable, params string[] properties) 
     { 
      if (properties.Any(x => validatable[x] != null)) 
       throw new InvalidOperationException("The object is invalid."); 
     } 
    } 

关闭到一个数据库表“产品”列名指定,它很好地工作。我在真实应用中使用这种技术,因为它很好地处理了数据库访问层,但我需要能够从我的对象访问多个表。我该如何做到这一点 - 是否需要将对象拆分为反映其表的层次结构,还是可以从主对象访问多个表,并将其他附加对象挂起并拥有自己的表?如果是这样,那么如何在对象和表之间创建ORM链接?

干杯

MH

回答

2

只需添加相关表格,你的资料库界面,就像你有一个产品,然后在你的仓库类中创建的具体实现,再次就像你有一个产品。

我用我的应用程序相同的模式,我有两个仓库,每个处理5-10表。有两组不同的表格是相关的,因此有两个存储库。

我将因此改变SQLRepository构造:

public SqlProductsRepository(string connectionString) 
    { 
     DataContext dc = new DataContext(connectionString); 
     productsTable = dc.GetTable<Product>(); 
    } 

然后,您可以轻松地扩展它因而例如:

private Table<Order> ordersTable; 

    public SqlProductsRepository(string connectionString) 
    { 
     DataContext dc = new DataContext(connectionString); 
     productsTable = dc.GetTable<Product>(); 
     ordersTable = dc.GetTable<Order>(); 
    } 

    IQueryable<Order> Orders 
    { 
     get { return from o in ordersTable select o; } 
    } 

编辑 - 回答评论

下面是如何提供一个例子通过此方法从属对象(相关表格):

[Table(Name="Projects")] 
public class Project 
{ 
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] 
    public Guid ID { get; set; } 
    [Column] 
    public String Name { get; set; } 
    [Column] 
    public bool Active { get; set; } 

    [Association(ThisKey="ID", OtherKey = "ProjectID")] 
    private EntitySet<ProjectDate> _projectDates = new EntitySet<ProjectDate>(); 
    public IQueryable<ProjectDate> ProjectDates 
    { 
     get { return _projectDates.AsQueryable(); } 
    } 
} 

而对于完整性ProjectDate类

[Table(Name="ProjectDates")] 
public class ProjectDate 
{ 
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] 
    public Guid ID { get; set; } 
    [Column] 
    public Guid ProjectID { get; set; } 
    [Column] 
    public DateTime TargetDate { get; set; } 
    [Column(CanBeNull = true)] 
    public DateTime? ActualDate { get; set; } 
    [Column(CanBeNull=true, IsDbGenerated = true)] 
    public DateTime? Created { get; set; } 

    private EntityRef<Project> _project; 
    [Association(ThisKey = "ProjectID", Storage = "_project", OtherKey = "ID")] 
    public Project Project 
    { 
     get { return _project.Entity; } 
     set { _project.Entity = value; ProjectID = _project.Entity.ID; } 
    } 
} 
+0

感谢您的。 对不起,我很愚蠢。我将如何创建主对象中两个表之间的关系(例如,您可以访问产品的订单集合,例如Product.Orders – 2009-08-20 08:48:44

+0

请参阅上面的 – Lazarus 2009-08-20 13:38:49

+0

以上的增加,非常感谢,我已经得到了一些用户界面代码可以编写,但随后我会继续尝试。 – 2009-08-21 08:54:23