我的工作我的方式,通过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