Newbie question: need help with ContextTypeName

I followed the Contoso University tutorial on asp.net and everything works fine. The next step was to repeat some things on my new website. I added an EntityDataSource object 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 recommended that you replace ConnectionString and DefaultContainerName with ContextTypeName.

โ€œIn the layout of the EntityDataSource control, remove the ConnectionString and DefaultContainerName attributes and replace them with the 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, different from the one that is hardcoded in the feature class. "

It helped me in a textbook where they had:

  <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 does not fit in the DAL folder. Instead, I accepted the default folder name recommendation for Visual Web Developer. I believe this is "App_Code". But ContextTypeName = "MyWebsite2011.App_Code.MyWebsite2011Entities" does not work. When I launch the browser, it complains that the type MyWebsite2011.App_Code.MyWebsite2011Entities cannot 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? As I said, ConnectionString and DefaultContainerName worked, so I think MyWebsite2011Entities is ok. Any clues would be appreciated because I don't know the naming convention or what to look for.

+6
source share
2 answers

Open the .cs file in which the context is declared, and look at the text immediately after the namespace declaration. This is the namespace of your class. Your ContextTypeName should be <namespace>.<classname> (without the brackets <>, of course.)

+11
source

I followed the same Contoso University tutorial, but I used Windows Azure as an online database.
Therefore, I also ran into this problem.
For other people interested in how to fix this when using the online database, I did the following.

With your EntityDataSource, do:
Delete the following:

ConnectionString = ...
DefaultContainerName = ...

Add

 OnContextCreating="EntityDataSource_ContextCreating" 

Leave 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 is your username. If we look at the original question, it will be MyWebsite2011Entities.

0
source

Source: https://habr.com/ru/post/888686/


All Articles