雖然沒有直接回答你的問題,我想出了一個基於T4解決各地MergeOption不是在DbSet訪問的EF監督。從你的問題來看,這就是你正在尋找的東西?
在默認環境下,你必須用T4發電機等產生的東西:
public virtual DbSet<Person> Persons { get; set; }
public virtual DbSet<Address> Addresses { get; set; }
等
我的方法是編輯T4添加吸氣劑爲每個實體提供直接訪問的對象集作爲一個IQueryable:
public IQueryable<Person> GetPersons(MergeOption mergeOption = MergeOption.AppendOnly, bool useQueryImplentation = true)
{
return useQueryImplementation ? GetSet<Person>(mergeOption).QueryImplementation() : GetSet<Person>(mergeOption);
}
然後在鹼的DataContext
public class DataContextBase
{
/// <summary>
/// Gets or sets the forced MergeOption. When this is set all queries
/// generated using GetObjectSet will use this value
/// </summary>
public MergeOption? MergeOption { get; set; }
/// <summary>
/// Gets an ObjectSet of type T optionally providing a MergeOption.
/// <remarks>Warning: if a DataContext.MergeOption is specified it will take precedence over the passed value</remarks>
/// </summary>
/// <typeparam name="TEntity">ObjectSet entity Type</typeparam>
/// <param name="mergeOption">The MergeOption for the query (overriden by DataContext.MergeOption)</param>
protected IQueryable<TEntity> GetObjectSet<TEntity>(MergeOption? mergeOption = null) where TEntity : class
{
var set = Context.CreateObjectSet<TEntity>();
set.MergeOption = MergeOption ?? mergeOption ?? MergeOption.AppendOnly;
return set;
}
通過創建一個IQueryable一個默認的擴展方法如下,你可以選擇添加自己QueryImplementation的implenations爲每個表/類型,以便您的表的所有用戶獲得排序或包括等(不需要這部分要回答這個問題,但無論如何它是有用的)
因此,舉例來說,你可以添加以下調用GetPersons()
public static class CustomQueryImplentations
{
public static IQueryable<Person> QueryImplementation(this IQueryable<Person> source)
{
return source
.Include(r => r.Addresses)
.OrderByDescending(c => c.Name);
}
}
最後時,始終包括地址:
//just load a simple list with no tracking (Fast!)
var people = Database.GetPersons(MergeOption.NoTracking);
//user wants to edit Person so now need Attached Tracked Person (Slow)
var peson = Database.GetPersons(MergeOption.OverwriteChanges).FirstOrDefault(p => p.PersonID = 1);
//user makes changes and on another machine sometime later user clicks refresh
var people = Database.GetPersons(MergeOption.OverwriteChanges);
或者你也可以(如我)喜歡寫東西使用現有的Get方法實體
Database.MergeOption = MergeOption.OverwriteChanges;
刷新加載,但所有的一切都會覆蓋已添加實體
Database.MergeOption = null;
一些需要注意的是如果您在進行更改之前加載AsNoTracking,則需要使用OverwriteChanges重新附加或可能更好的重新加載,以確保您擁有最新的實體。
有一個POCO生成器:https://visualstudiogallery.msdn.microsoft。com/66612113-549c-4a9e-a14a-f629ceb3f89a – ErikEJ 2015-03-02 10:19:32
@ErikEJ是的,我意識到這一點,但我不認爲它創建POCO,而是創建EntityObject派生類?當我使用它時,它只創建了xxModel.tt文件,而不像x4Model.Context.tt文件那樣在EF4.4版本的T4中創建。 – GHauan 2015-03-02 14:48:59
對不起,你是對的...... – ErikEJ 2015-03-02 16:49:01