![](/img/trans.png)
[英]Troubles making PCL Xamarin (MonoDroid/MonoTouch) compliant
[英]Sharing EF 6.0 PCL in Webapp, xamarin Monotouch IPAD?
我們首先使用EF 6模型開發了一個Web應用程序。 此模型及其業務邏輯位於單獨的PCL項目中。 是否可以在使用本地DB的Xamarin.monotouch應用程序中使用編譯的dll? 如果是,怎么辦? 有誰知道關於這個主題的好文章? 如果不是,是否有人知道此問題的解決方法?
我嘗試了Google搜索,但未找到任何相關結果。
有兩種方案-代碼優先或模型優先。 兩者都歸結為條件編譯。
代碼優先要容易得多:
創建您的EF項目(√完成)
創建另一個(PCL)項目,在其中導入相同的模型類進行編譯。 制作您需要的其他PCL類型的項目。
在PCL項目中,添加一個編譯器符號。 在Xamarin Studio的項目options / build / compiler [Define Symbols]中找到它。 添加一個新符號,例如“ PCL”
在您的EF代碼中,將所有無法編譯的項目
#if !PCL
public DbSet<Users> {...}
#endif
您還可以添加僅在PCL中顯示或具有其他定義的代碼:
#if !PCL
public ICollection<User> Users{get;set;}
#endif
#if PCL
public List<User> Users{get;set;}
#endif
更困難的情況是Model First :
class
或using
或namespace
或ICollection
using
熟悉模板 <PropertyGroup>
,用它替換EF-copy項目的頂部屬性組。 那里有使項目成為PCL的設置,包括<TargetFrameworkProfile>
<Import Project=...>
標簽從新的PCL項目復制到EF-copy項目中,替換現有的 T4代碼修改 :在我自己的.tt文件中,我在第50行附近編輯了原始文件,生成了構造函數
if (collectionNavigationProperties.Any()){
#>
#if !PCL
<#
foreach (var navigationProperty in collectionNavigationProperties)
{
#>
this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
<# } #>
#endif
<#
}
然后在第100行,生成類屬性
#if !PCL
<# // this looks like the original .tt
foreach (var navigationProperty in navigationProperties)
{
#>
<#=codeStringGenerator.NavigationProperty(navigationProperty)#>
<#
}
#>
#endif
#if PCL
<# // and this is the copy I've added. Notice it calls a different method, see below
foreach (var navigationProperty in navigationProperties)
{
#>
<#=codeStringGenerator.DeserializeNavigationProperty(navigationProperty)#>
<#
}
#>
#endif
在.tt中的NavigationProperty
方法之后,我添加了以下內容:
public string DeserializeNavigationProperty(NavigationProperty navigationProperty)
{
string navPropCode = NavigationProperty(navigationProperty);
navPropCode = navPropCode.Replace("ICollection","List");
return navPropCode;
}
用List
方式替換ICollection
,我有一個反序列化的具體類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.