2017-03-03 66 views
1

我試着OrmLite看看我是否可以在我的項目中替換實體框架。速度對簡單查詢來說非常重要。但我試圖映射/引用[1到多關係,並閱讀文檔+從github頁面檢查測試代碼,但沒有成功。這是我的例子。有沒有我忘記或應該做的事情來讓它像Entity Framework一樣工作?ServiceStack OrmLite映射引用不工作

// 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; } 
} 

回答

2

理想情況下,您的POCOs/DTOs shouldn't use interfaces,您不需要使用virtual作爲ORM只填充您自己的POCO(即,它不會產生像其他重型奧姆斯你的模型的代理),我也喜歡使用[AutoIncrement]整數IDS(除非你需要填充特定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上的後向引用,因爲它不會被填充。

您還需要才能使用LoadSelectquery and return POCOs with references,所以他們StockItem引用返回產品,你可以這樣做:

db.LoadSelect<Product>(); 

您也可以填寫此手動2查詢通過using Merge extension method to merge 2 disconnected record sets,如:

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

products.Merge(stockItems); 

這將合併與他們StockItems產品,你可以快速運行看:

products.PrintDump(); 
+2

Thx @mythz。正是我在找的東西。很棒。我將繼續轉換我的項目並用OrmLite替換EF。性能提升非常顯着。 – Nesse