有什麼方法可以爲Linq2Entities的附加功能定義SQL轉換組件。添加新的方法到LINQ to Entities
例如:
myQuery.Where(entity => entity.Contains('foo', SearchFlags.All))
理想我要尋找的東西,不需要編輯,並直接建立一個新版本的EntityFramework.dll。有什麼辦法讓擴展方法可以支持SQL生成的實體框架。
到目前爲止,我有這將意味着我需要爲LINQ更換給實體的方法的模板:
public static bool Contains(this object source, string searchTerms, SearchFlags flags)
{
return true;
}
當然,這將導致錯誤:
LINQ to Entities does not recognize the method 'Boolean CONTAINS(System.Object, System.String, SearchFlags)' method, and this method cannot be translated into a store expression.
要清楚,我不想做:
myQuery.AsEnumerable().Where(entity => entity.Contains('foo', SearchFlags.All))
因爲我想能夠在SQL空間中執行代碼而不是r手動刪除所有實體。
我也不能使用IQueryable
的.ToString()
並手動執行它,因爲我需要實體框架來填充幾個.Include
連接的對象。
「* Model-declared functions *」(http://msdn.microsoft.com/en-us/library/ee382839.aspx)或「* Model-defined functions *」(http://msdn.microsoft.com/en-us/library/default.aspx)。 com/en-us/library/ee382833.aspx)可能是您可以嘗試的一種方法。 Code-First不支持這兩種工具,只有基於EDMX的工作流(DB-First或Model-First),後者需要使用不具有所有SQL功能的實體SQL。 – Slauma
@Slauma看起來很有用,但是我堅持使用代碼優先,我可以用它們來爭辯我們應該先使用DB-first,因爲我們無論如何都要將DB分開管理DB – Seph
您運行的代碼是針對哪種SQL Server版本的? – Marcel