Subsonic 亚音速3,如何使用LinqTemplates积垢?

Subsonic 亚音速3,如何使用LinqTemplates积垢?,subsonic,subsonic3,Subsonic,Subsonic3,我是亚音速新手,似乎无法找到使用LINQ模板类执行CRUD操作的自然方法。我想在ActiveRecord中,您可以: Product p = new Product(); p.ProductCode = "xxx"; p.Add(); 但是,使用LINQTemplate生成的类,我如何做同样的事情?我只能使用下面这样的方法插入产品对象: db.Insert.Into<UnleashedSaaS.PRODUCT>(prod => prod.Code, prod =>

我是亚音速新手,似乎无法找到使用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();
db.Insert.Into(prod=>prod.code,prod=>prod.Description).value(“Product1”,“Product1 Desc”).Execute();

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

所有积垢都发生在您可以从中派生的存储库中。例如,我会有这样一个类:

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);
    }
}
公共类产品存储库:子存储库{
public ProductRepository():base(新NorthwindDB()){}
//这里需要这个,因为base不公开我所知道的DB类
受保护的NorthwindDB_db;
受保护的NorthwindDB{
得到{
如果(_db==null)_db=new NorthwindDB();
返回_db;
}
}
公共作废保存(产品){
if(product.ProductId==0){
添加(产品);//添加是存储库的一部分
}否则{
更新(产品);
}
}
公共无效删除(产品){…}
公共列表ListAll(){
var products=以DB.products表示的p
选择p;
返回产品。ToList();
}
公共产品GetById(int-id){
返回DB.GetByKey(id);
}
}
等等。这很好,因为您可以在一个地方整合所有数据访问方法。如果您有存储过程,它们也会作为DB上的方法生成


当我有时间时,我将直接向存储库添加一个Save方法,这样您就不必亲自检查要调用哪个方法(Add或Update)。

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

public partial class <#=tbl.ClassName#>Repository : SubSonicRepository<<#=tbl.ClassName#>>
    {
        public <#=tbl.ClassName#>Repository() : base(new <#=DatabaseName#>DB()) { }
    }
这将生成一个存储库,允许您访问基本功能,但可以在另一个分部类中进行修改


希望有帮助。

+1。整个上午我都在想同样的事情。我不明白为什么这不是内置的和容易做到的。看起来ActiveRecord比Linq的东西要完整得多。
<# foreach(Table tbl in tables){#>
/// <summary>
using System.Linq;
using SubSonic.Repository;