親愛的程序員容易結合,的DbContext的有或無WCF
我有,我不能成功找出問題,更關係到我應該如何設計的應用程序,到現在爲止我總是通過編寫大量代碼來克服這個問題。
我必須設計一個連接到數據庫的silverlight應用程序,我想在這兩個之間使用WCF服務有很多原因。
如果我使用WCF服務,我將放棄dbcontext結構,當我們想要使用CollectionViewSource和相關字段來綁定xaml控件時,它非常好。
例如,讓我們說我們有簡單的實體,如認爲,從EDMX文件生成:電阻和ResistorCategories
public partial class ResistorCategories
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ResistorCategories()
{
this.Name = "New";
this.ResistorsSet = new HashSet<ResistorsSet>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ResistorsSet> ResistorsSet { get; set; }
}
public partial class ResistorsSet
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ResistorsSet()
{
this.ResistorStockEntriesSet = new HashSet<ResistorStockEntriesSet>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Resistance { get; set; }
public Nullable<decimal> PowerRating { get; set; }
public Nullable<decimal> Price { get; set; }
public Nullable<decimal> Tolerance { get; set; }
public string SupplierCode { get; set; }
public string ManufacturerCode { get; set; }
public int ResistorCategories_Id { get; set; }
public Nullable<int> Suppliers_Id { get; set; }
public Nullable<int> Manufacturers_Id { get; set; }
public virtual ResistorCategories ResistorCategories { get; set; }
public virtual Suppliers Suppliers { get; set; }
public virtual Manufacturers Manufacturers { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ResistorStockEntriesSet> ResistorStockEntriesSet { get; set; }
}
在XAML,這是很容易放置2 DataGrid和看名單
在XAML:
的屬於一個類別電阻n個I每個DataGrid綁定到其相應的CollectionViewSource:
ItemsSource="{Binding Source={StaticResource resistorCategoriesViewSource}, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Source={StaticResource resistorCategoriesResistorsViewSource}, UpdateSourceTrigger=PropertyChanged}"
而在代碼隱藏文件,用惰性加載啓用:
System.Windows.Data.CollectionViewSource resistorCategoriesViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("resistorCategoriesViewSource")));
//
_context.ResistorCategoriesSet.Load();
resistorCategoriesViewSource.Source = _context.ResistorCategoriesSet.Local;
因此,它不能更容易顯示他們的相關領域的記錄,但在這種情況下,xaml客戶端直接查詢數據庫,這是我想通過WCF服務避免的,並且增加了一些標準化和安全性。
對於WCF,首先必須使用EF Power Tool生成POCO對象,因爲它無法通過示例返回IEnumerable,因爲它的虛擬屬性(即使我禁用了惰性加載和代理)。 後來我有分離的集合查詢和存儲的POCO對象: 的ObservableCollection & &的ObservableCollection
然後我每個DataGrid綁定到其收集和監控將selectedItem事件手動更改電阻器顯示在數據網格收藏!
所以我想有一個解決方案使用WCF和簡單的dbcontext! 請幫幫我!在此先感謝
你使用什麼樣的數據庫? MySql,MsSQL,Postgres? –
嗨,Thx爲您的留言!我實際上在此項目中使用SQLEXPRESS,通常我大部分時間都使用MySql。我還沒有找到解決方案,所以我決定在其他地方節省時間:我已經實現了wcf數據服務,所以我幾乎不必寫wcf服務,我可以使用集成機制來保護它。我也可以使用它作爲xaml內的數據源,所以我只需拖放表單和數據網格即可。當然,最後一件事是關於我的帖子,我必須加載並手動進行綁定! –