我的工作我的方式,通過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
感謝您的。 對不起,我很愚蠢。我將如何創建主對象中兩個表之間的關係(例如,您可以訪問產品的訂單集合,例如Product.Orders – 2009-08-20 08:48:44
請參閱上面的 – Lazarus 2009-08-20 13:38:49
以上的增加,非常感謝,我已經得到了一些用戶界面代碼可以編寫,但隨後我會繼續嘗試。 – 2009-08-21 08:54:23