2009-08-19 80 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