【问题标题】:ServiceStack OrmLite mapping with references not working引用不起作用的 ServiceStack OrmLite 映射
【发布时间】:2017-03-03 08:13:37
【问题描述】:

我正在试用 OrmLite,看看是否可以在我的项目中替换实体框架。简单查询的速度非常显着。但是我试图映射/引用一个 [1 to many- 关系并阅读文档+检查来自 github 页面的测试代码,但没有成功。这是我的例子。有什么我忘记或应该做的事情来让它像实体框架一样工作吗?

示例

// EF: returns +15.000 records + mapped > product.StockItems (slow)
dbContext.Products.Include(x => x.StockItems).ToList();

// OrmLite: returns +100.000 records (NO mapping > product.StockItems)  
db.Select<Product>(db.From<Product>().Join<StockItem>());

// OrmLite: +15.000 separate requests to sql server (bad workarround + slow)
foreach (var product in db.Select<Product>())
{
    // manual mapping
    product.StockItems = db.Select<StockItem>(x => x.ProductId == product.Id);
}

Product.cs

public class Product
{
    public int Id { get; set; }
    public ProductType ProductType { get; set; }         
    public string Name { get; set; }       
    public string Description { get; set; }     
    public int DisplayOrder { get; set; }
    public bool LimitedToStores { get; set; }
    public string Sku { get; set; }
    public decimal Price { get; set; }
    public decimal OldPrice { get; set; }
    public decimal SpecialPrice { get; set; }
    public decimal DiscountPercentage { get; set; }       
    public DateTime? DateChanged { get; set; }
    public DateTime? DateCreated { get; set; }
    //...

    [Reference] 
    public virtual IList<StockItem> StockItems { get; set; } = new List<StockItem>();

}

StockItem.cs

public class StockItem
{
    public int Id {get; set;}
    [References(typeof(Product))]
    public int ProductId { get; set; }
    public string Size { get; set; } 
    public int TotalStockQuantity { get; set; }   
    public string Gtin { get; set; }
    public int DisplayOrder { get; set; }
    // ...

    [Reference] 
    public virtual Product Product { get; set; }
}

【问题讨论】:

    标签: c# entity-framework servicestack ormlite-servicestack


    【解决方案1】:

    理想情况下是您的POCOs/DTOs shouldn't use interfaces,并且您不需要使用virtual,因为ORM 仅填充您自己的POCO(即它不会像其他重型ORM 那样创建您的模型的代理),我也更喜欢使用@987654326 @ 表示整数 ID(除非您需要填充特定的 ID),所以我的模型看起来像:

    public class Product
    {
        [AutoIncrement]
        public int Id { get; set; }
        public ProductType ProductType { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public int DisplayOrder { get; set; }
        public bool LimitedToStores { get; set; }
        public string Sku { get; set; }
        public decimal Price { get; set; }
        public decimal OldPrice { get; set; }
        public decimal SpecialPrice { get; set; }
        public decimal DiscountPercentage { get; set; }
        public DateTime? DateChanged { get; set; }
        public DateTime? DateCreated { get; set; }
    
        [Reference]
        public List<StockItem> StockItems { get; set; }
    }
    
    public class StockItem
    {
        [AutoIncrement]
        public int Id { get; set; }
        [References(typeof(Product))]
        public int ProductId { get; set; }
        public string Size { get; set; }
        public int TotalStockQuantity { get; set; }
        public string Gtin { get; set; }
        public int DisplayOrder { get; set; }
    }
    

    OrmLite's POCO References 仅填充 1 级深度,具有循环关系不是一个好主意,因为它们不可序列化,因此我将删除 StockItems 上的反向引用,因为它不会被填充。

    您还需要使用LoadSelect 才能使用query and return POCOs with references,因此要返回带有 StockItem 引用的 Product,您可以这样做:

    db.LoadSelect<Product>();
    

    您也可以使用using Merge extension method to merge 2 disconnected record sets2 个查询 手动填充此内容,例如:

    var q = db.From<Product>().Join<StockItem>();
    var products = db.Select(q.SelectDistinct());
    var stockItems = db.Select<StockItem>();
    
    products.Merge(stockItems);
    

    这会将产品与其 StockItems 合并,您可以通过运行快速查看:

    products.PrintDump();
    

    【讨论】:

    • 谢谢@mythz。正是我想要的。效果很好。我将继续转换我的项目并将 EF 替换为 OrmLite。性能提升非常显着。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多