2011-05-21 123 views
6

我正在關注asp.net網站上的Contoso University教程,它一切正常。我的下一步是在我自己的新網站上覆制一些內容。我加入的作品的EntityDataSource:新手問題:需要關於ContextTypeName的幫助

<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server" 
     ConnectionString="name=MyWebsite2011Entities" 
     DefaultContainerName="MyWebsite2011Entities" EnableFlattening="False" 
     EntitySetName="product_type"> 

而按照教程這是一個ContextTypeName更換的ConnectionString和DefaultContainerName一個好主意。

「在EntityDataSource控件的標記中,刪除ConnectionString和DefaultContainerName屬性,並用ContextTypeName =」ContosoUniversity.DAL.SchoolEntities「屬性替換它們。這是每次創建EntityDataSource控件時應該進行的更改,除非您需要使用與在對象上下文類中硬編碼的連接不同的連接。「

,在那裏他們有一個教程的工作對我罰款:

<asp:EntityDataSource ID="StudentsEntityDataSource" runat="server" 
     ContextTypeName="ContosoUniversity.DAL.SchoolEntities" 
     EnableFlattening="False" 
     EntitySetName="People" 
     EnableDelete="True" EnableUpdate="True"> 
    </asp:EntityDataSource> 

對我來說,區別(除了項目名稱)是我的實體模型不是放置在DAL文件夾中。相反,我接受了Visual Web Developer的默認文件夾名稱建議。我相信這是「App_Code」。但是,ContextTypeName =「MyWebsite2011.App_Code.MyWebsite2011Entities」不起作用。當我啓動瀏覽器時,它抱怨MyWebsite2011.App_Code.MyWebsite2011Entities類型無法讀取。

<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server" 
     ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities" 
     EnableFlattening="False" EntitySetName="product_type"> 

如何找到放入的正確ContextTypeName?正如我所說的,ConnectionString和DefaultContainerName的工作,所以我猜MyWebsite2011Entities是好的。任何提示將不勝感激,因爲我不知道命名約定或尋找什麼。

+0

ContextTypeName引用您的namespace.className而不是文件名。 – 2011-05-21 07:28:54

回答

11

打開聲明Context的.cs文件,並在namespace聲明後立即查看文本。這是你的班級的名字空間。您的ContextTypeName應爲<namespace>.<classname>(當然,不包含<>方括號。)

+0

我在App_Code/MyWebsite2011.edmx/MyWebsite2011.Designer.cs中找到它。事實證明,正確的ContextTypeName是MyWebsite2011Model.MyWebsite2011Entities。非常感謝你。 – 2011-05-21 07:33:09

+2

@Kasper沒問題!隨意標記這是正確的答案。 – dlev 2011-05-21 07:35:07

0

我在使用相同的Contoso大學教程,但我使用的是Windows Azure作爲在線數據庫。
所以我也遇到了這個問題。
對於使用在線數據庫時如何解決這個問題的其他人,我做了以下工作。

與您EntityDataSource做:
拆卸:

的ConnectionString = ...
DefaultContainerName = ...

地址:

OnContextCreating="EntityDataSource_ContextCreating"  

離開ContextTypeName空。
最後落實相應的.cs下面的代碼:

protected void EntityDataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e) 
{ 
    <ContainerName> context = new <ContainerName>(); 
    e.Context = ((IObjectContextAdapter)context).ObjectContext; 
} 

容器名稱是ofcourse您的容器名稱。
如果我們看一下原來的問題,那麼它將是MyWebsite2011Entities。