Newbie question: Need help with ContextTypeName
I was following the Contoso University tutorial on the asp.net website and it all works well. My next step was to replicate some of the things on my own new website. I have added an EntityDataSource that works:
<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server"
ConnectionString="name=MyWebsite2011Entities"
DefaultContainerName="MyWebsite2011Entities" EnableFlattening="False"
EntitySetName="product_type">
And according to the tutorial it is a good idea开发者_高级运维 to replace the ConnectionString and DefaultContainerName with a ContextTypeName.
"In the markup for the EntityDataSource control, remove the ConnectionString and DefaultContainerName attributes and replace them with a ContextTypeName="ContosoUniversity.DAL.SchoolEntities" attribute. This is a change you should make every time you create an EntityDataSource control, unless you need to use a connection that is different from the one that's hard-coded in the object context class."
That worked fine for me in the tutorial where they had a:
<asp:EntityDataSource ID="StudentsEntityDataSource" runat="server"
ContextTypeName="ContosoUniversity.DAL.SchoolEntities"
EnableFlattening="False"
EntitySetName="People"
EnableDelete="True" EnableUpdate="True">
</asp:EntityDataSource>
The difference for me (besides the project name) is that my entity model is not placed in a DAL folder. Instead I accepted the Visual Web Developer's default folder name recommendation. I believe it was "App_Code". But the ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities" doesn't work. When I start the browser, it complains that the type MyWebsite2011.App_Code.MyWebsite2011Entities could not be read.
<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server"
ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities"
EnableFlattening="False" EntitySetName="product_type">
How do I find the correct ContextTypeName to put in? As I said, the ConnectionString and DefaultContainerName worked, so I guess the MyWebsite2011Entities is ok. Any hints would be appreciated because I don't know the naming convention or what to look for.
Open up the .cs file in which the Context is declared, and look at text immediately after the namespace
declaration. That's your class's namespace. Your ContextTypeName should be <namespace>.<classname>
(without the <> brackets, of course.)
I was following the same Contoso University tutorial but I was using windows Azure as an online Database.
So I also ran into this problem.
For other people interested on how to fix this when using an online database, I did the following.
With your EntityDataSource do:
Remove the following:
ConnectionString=...
DefaultContainerName=...
Add:
OnContextCreating="EntityDataSource_ContextCreating"
Leaving ContextTypeName empty.
And finally implement the following code in the corresponding .cs:
protected void EntityDataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
<ContainerName> context = new <ContainerName>();
e.Context = ((IObjectContextAdapter)context).ObjectContext;
}
ContainerName being your containername ofcourse.
If we would look at the original question then it would be MyWebsite2011Entities.
精彩评论