EF6 only lazy works from loaded context data - entity-framework

Using EF 6 rc 1
Code first
Lazy loading is enabled and proxy generation is allowed (defaults).
When attempting to access navigation properties, I am finding that:
- if the referenced object was already loaded into the DbContext in some manner, the navigation property will be populated
- if the referenced object was not already loaded, the navigation property will remain null. No query attempt is made to the SQL Server DB.
This applies to collections and objects. In all cases, I have verified that the navigation properties are marked virtual.
Is this expected behaviour? I have scroured every article I could find but have found no description of this (please provide links). If not expected behaviour, what could I be doing wrong?
Thank you!
EDIT:
Because I can't post the classes I have, I've tried to reproduce this into a simple example I would be comfortable posting. The simple example, however, works as expected. I have noticed one major difference between the working example and the non-working: my non-working application produces entities that are NOT proxies. This, even though proxy generation is allowed, and I am retrieving these objects from a new context (NOT the context they were created in). I can't post the non-working code, and there's no value in posting the working code. Theories explaining why proxy classes are not being generated would be welcome!

Turned out the reason the Proxy wrapper wasn't being created is because some of the classes had the default constructor marked internal. Simply changing them to protected allowed the Proxy to be created, which then enabled lazy loading. Though I found this to be pretty obscure it is documented here: http://msdn.microsoft.com/en-us/library/vstudio/dd468057(v=vs.100).aspx

Related

Topic "Troubleshooting" no response inside browser console

I cannot get this to work as stated in the UI5 documentation topic Troubleshooting for browser developer tools.
I already typed the code but I see no response.
myView=sap.ui.getCore().byId("HeapOfShards---app").
myView.byId("myButton").
Issue 1
When pasting code snippet from the documentation, you need to omit the full stop (.).
Otherwise, the console will think that the expression is not complete, hence, no response.
But just omitting the full stop will lead to an error that myView is undefined. See Issue 2.
Issue 2
Since UI5 1.61.0, the component in the sample code (id="HeapOfShards") gets an additional auto-prefixed ID from the parent ComponentContainer (id="container"). Therefore, when accessing the root view via the global ID like in the topic, the ID of the ComponentContainer needs to be prepended:
myView = sap.ui.getCore().byId("container-HeapOfShards---app");
myView.byId("myButton");
This is clearly a documentation issue that needs to be fixed. Update: It's fixed now.
Why it didn't work
In 1.61, the sample code for Troubleshooting moved from creating the ComponentContainer via inline-script to creating it declaratively via the module sap/ui/core/ComponentSupport.commit
The reason why .byId("HeapOfShards---app") suddenly failed is that ComponentSupport sets several settings beforehand when creating the ComponentContainer, among which the autoPrefixId gets true by default. src
When creating the ComponentContainer directly without the ComponentSupport, autoPrefixId is false by default which is why it was all fine in previous versions.

SAPUI5 Smartform Fields Not Editable

I have managed to setup a Smartform for update / delete purposes. Binding works alright, fields are visible but all of them are read only. Of course, form editable attribute is set to true.
OData model looks good:
creatable, updatable, and deletable flags are checked for the entity set
All fields have the creatable flag
All fields, except the keys, have the updatable flag in entity type.
Backend methods are in place and tested in the gateway client. Followed the steps required to clear the server cache (without being sure it's required) but situation remains the same.
Is there something that I'm missing or a place that I forgot to have a look here?
​Solution found and had nothing to do with my coding. It was all about closing SAP Personal Web IDE, stopping and restarting the Orion service, logging in again and just rerunning the application. Form behaves now as supposed without alerting a single line of code...

trouble scaffolding entity framework based controller in ASP.Net Core 2.0

I'm using Visual Studio 2017, v15.3.1 and am trying to follow a tutorial about creating some WebAPI endpoints with Entity Framework, as outlined here
In my case I'm using the good old Northwind SQL database instead of the author's blogging example, which I don't think should be an issue?
At any rate, when I get to the bit where I'm supposed to create a controller, I right-click the controllers folder in my solution, select 'API Controller with actions, using Entity Framework' and choose my EF model class (Orders, Employees, it doesn't seem to matter) and the Data Context which I generated successfully earlier in the tutorial.
When I select Add there's an attempt to scaffold, but I'm then told in a popup: There was an error running the selected code generator: 'No parameterless constructor defined for this object'.
I've looked at this error message online and others have had similar problems, but nothing they tried quite matches my scenario, and their solutions haven't fixed this issue.
My generated EF model classes do have parameterless constructors defined, so I'm not sure what the error is referring to?

add scaffolded item wizard cannot resolve data model class

everyone.
I'm trying to create new Scaffolded item for simple ASP.NET Wep API application which should be based on domain object class and DbContext derivative in separate assembly. The assembly is in the solution, target app has a reference to it (and manually created code which invokes the classes from my lib is build up without any errors) and, obviously, the classes I've mentioned have access modifier public.
The problem is wizard for creating new scaffolded item cannot see my model classes. (By the way, when the model classes were in another ASP.NET MVC5 app the wizard worked well.) I'm using Visual Studio 2013 Update5.
How to fix this? Any workaround would be helpful too!
Sorry for disturbing.
the origin of the problem seems to be that I've move my data model classes from ASP.NET MVC app into my class library in wrong way.
I've not just cut/paste them, but copy-paste, cut-paste (confirmed replacement).
this is the only thing which could cause the problem (although I still don't know the details).
Never the less I've started from the beginning again and cut/past the data model files initially - everything seems to work fine now.

MVC Default Edit View increments the id property of my model? Can't figure out why. .

I'm trying to learn web development, and I bet that this is a simple problem and that I'm overlooking something obvious.
In my default crud edit controller (generated using the MVC framework) I retrieve my model from a localDB instance using my EntityFramework's dbContext. That is sent to the View. In the debugger just before the controller call to return View(model) I can see that the Id is set to 2.
When the [post] edit controller is fired I see that the identity property is 3. I wanted to figure out why so I changed the View to display my Id property and I see that it is 3 as soon as I render the page. Last time I saw it the property was 2, now it is 3.
I don't know how to hook into any logic that would happen between the time I send off my model and when when the view is rendered.
Can anyone help me learn how to debug this so that I can figure out why my Id property is incremented when I pass the model into the view?
I don't know how EntityFramework works but I have worked with CakePHP. My advice is like this:
Usually PHP frameworks have debug mode which you can set in the configuration file (turn it on or off). Usually stack of operation executed is also displayed in debug mode or there is a simple way to do that. There's also for example in CakePHP exist function debug($yourVariable); try to search for sth. similar
It seams like not the edit happens but new row is inserted to the database. Check your database for this. I recommend to debug the id of the column being passed for edit action and check if there's the same id in the database first of all.
Hope something helps.
I figured it out. Posting here for anyone else that comes across my problem.
I tried dumping the whole model out without using any of the htmlhelper methods. (In my case I am uinging: Html.HiddenFor helper) When I did this I saw that the value in the model was what I expected it to be. So I began investigating why the helper methods might be broken. Google worked for me here :)
Turns out, when the helper methods run they first check the ModelState dictionary for the desired value. In my case I was looking for a value that was in my model object as well as the ModelState dictionary, because the name was very common: Id.
To fix the issue before I call return View(model) I call ModelState.Clear() in order to make sure there are no values in the dictionary that are in conflict. Doing this causes my page to be rendered correctly.