2

我是 Subsonic 的新手,似乎找不到使用 LINQ 模板类进行 CRUD 操作的自然方法。我猜在 ActiveRecord 中,你可以:

Product p = new Product(); 
p.ProductCode = "xxx"; 
p.Add(); 

但是,使用 LINQTemplate 生成的类,我该如何做同样的事情?我只能使用下面这样的东西来插入产品对象:

db.Insert.Into<UnleashedSaaS.PRODUCT>(prod => prod.Code, prod => prod.Description).Values("Product1", "Product1 Desc").Execute();

谁能给我一些提示?我真的很感激。

4

2 回答 2

2

所有 CRUD 都发生在 SubSonicRepository 中,您可以从中派生。例如,我会有这样的课程:

public class ProductRepository : SubSonicRepository<Product> {

    public ProductRepository() : base(new NorthwindDB()) { }

    // need this here because base doesn't expose the DB class that I know of
    protected NorthwindDB _db;
    protected NorthwindDB DB {
        get {
            if (_db == null) _db = new NorthwindDB();
            return _db;
        }
    }

    public void Save(Product product) {
        if (product.ProductId == 0) {
            Add(product); // Add is part of SubSonicRepository
        } else {
            Update(product);
        }
    }

    public void Delete(Product product) { ... }

    public List<Product> ListAll() {
        var products = from p in DB.Products
                       select p;

        return products.ToList();
    }

    public Product GetById(int id) {
        return DB.GetByKey(id);
    }
}

等等。这很好,因为您可以将所有数据访问方法整合到一个地方。如果您有 Sproc,它们也会作为 DB 上的方法生成。

当我有时间时,我将直接向 SubSonicRepository 添加 Save 方法,这样您就不必自己检查要调用的方法(添加或更新)。

于 2009-07-22T04:25:30.037 回答
0

我已修改 Classes.tt 文件以包括:

public partial class <#=tbl.ClassName#>Repository : SubSonicRepository<<#=tbl.ClassName#>>
    {
        public <#=tbl.ClassName#>Repository() : base(new <#=DatabaseName#>DB()) { }
    }

在中间插入那一堆线

<# foreach(Table tbl in tables){#>

/// <summary>

就在顶部,靠近命名空间声明,在我的文件中,它可以插入到第 18 行。

最后要做的是在第 10 行,System.Linq 语句之后的下一行添加另一个“使用”语句。现在它应该看起来像:

using System.Linq;
using SubSonic.Repository;

这将生成一个存储库,让您可以访问基本功能,但可以在另一个部分类中进行修改。

希望有帮助。

于 2009-08-05T03:48:24.400 回答