I have a service that use EF 6.1.3 to access to the database. I have POCO entities to store the results of EF, but the resutls are dynamicProxies instead of the POCO entity.
The problem is that when the service try to send to the client this dynamic proxy, I get an error receiving the http response.
I have tried to disabled the creation of the proxies entities in my dbContext, and then I receive my real POCO entity so I have no problems.
But really I don't know what is dynamic proxies and when is usuful to use them and when I can disabled them.
EDIT: I have disabled lazy loading.
Thanks.
When creating instances of POCO entity types, the Entity Framework
often creates instances of a dynamically generated derived type that
acts as a proxy for the entity. This proxy overrides some virtual
properties of the entity to insert hooks for performing actions
automatically when the property is accessed. For example, this
mechanism is used to support lazy loading of relationships.
Source: https://msdn.microsoft.com/en-us/data/jj592886.aspx
You will find everything you need in the above article!
Related
I'm working on an ASP.net Web API application with Autofac and Entity Framework.
I've been breaking apart different my service classes into smaller classes in order to make my code more testable and to make my code more SOLID.
I'm using Autofac to inject Entity Framework DbContext into my various helper classes. This becomes problematic because if I use entities queried from DbContext in two different helper classes, I get an error when Entity Framework tries to produce a query.
The error says that Entity Framework cannot produce a query with entities from two different instances of DbContext.
Clearly, the solution is that I need to configure Autofac so that the same instance of DbContext is injected into each of the helper classes, but I'm afraid that if I try to do this, I may get concurrency issues when this application gets deployed to a production environment and many people use it at once.
How do I configure Autofac so that when a request hits my application, my API helper classes all get the same instance of DbContext, but I don't have concurrency issues across multiple requests?
An alternative to the action filter recommended by the Autofac documentation (https://autofaccn.readthedocs.io/en/latest/faq/per-request-scope.html#no-per-request-filter-dependencies-in-web-api) see: "No Per-Request Filter Dependencies in Web API" and manually going to the DependencyResolver for others:
You could have a look at the Medhime DbContextScope unit of work provider. (https://www.nuget.org/packages/EntityFramework.DbContextScope/) compiled for both EF6 and EF Core.
The injected dependencies for your classes becomes a DbContextScopeFactory for the top level, and an AmbientDbContextLocator for your services. These don't "break" with Web API's limitation on the request lifetime scope. The ContextScopeFactory would be initialized once and supply the DbContext, while the locators will be fed that single instance.
It may be worth having a look at if managing context references across services and an API action prove clunky.
I used the EF 5.x DbContext Fluent Generator to generate my POCO classes but my properties are not coded as virtual. Don't you have to have that for tracking to occur? Why wouldn't the template already use virtual for properties?
Because we found that for the majority of users it was better to use snapshot change tracking rather than change tracking proxies. Change tracking proxies have their place in certain situations, but usually they add complexity without any real benefit. For more info see http://blog.oneunicorn.com/2011/11/24/why-are-the-dbcontext-t4-templates-so-different-from-the-ef4-poco-templates/ and http://blog.oneunicorn.com/2011/12/05/should-you-use-entity-framework-change-tracking-proxies/
I have using self tracking entities and I do the following:
1.- I create a new entity
2.- I modify one of its properties, for example entityName="Dummy"
After the modification, the state of the entity is still unchanged, so when I do the applychanges it does anything.
I try to set its state to modified, and then it saves the changes in the database. But in this way, I have a problem, because if I am not wrong, STE implement the INotifyPropertyChanged interface, so when I modify a property, is notify and this is a feature that I would like to have.
I am wrong? Perhaps when I change a property the entity it would not be changed its state.
Thanks.
Daimroc.
EDIT: this problem also occurs when I get the entities when I consume a WCF service which send me the results of a query to a database using EF 4.0.
EDIT2: I found my error.
The problems is that I have a dll project in which I have the tt file and the Self tracking entities.
I have a WCF service that has a reference to this project. This Service use a repository that use EF to access to the database.
I have a self host application in which I host the WCF service. This application has not a reference to the dll with the STE.
I use svcutil to create the service.cs.
I have the client, in which I add the service.cs. How the service.cs has the classes of my dll project with the STE. But there is a problem, this classes does not have all of this classes. I mean that for example has the ChangeTracker property, but their has not the MarkAs method and others.
So if I edit the service.cs to change the namespace in which is declared my classes and add a reference to the project with the STE classes, the I have access to all methods and also works as I expected.
So the problem is in the "STE classes" of my service, not in the STE classes of my dll project.
Why when I generate the service.cs don't generate the "complete classes"? This makes me add a reference to my dll project with the STE and edit the service.cs to delete the code with its STE classes that don't work as I expect. is there any way to have in the service.cs file the "good STE classes"?
Thanks.
Daimroc.
The problem is that I have a WCF service that I host in an WPF application. I run the self host application and with svcutil I create the Service.cs.
I add this Service.cs in my client application. In this Service.cs exists my
Your description is too confusing.
STEs make tight coupling between you service and the client. The only correct way to use STEs is to place them to separate assembly and share this assembly between client and service. When you create service reference for the client application with svcutil.exe you can use reference parameter to specify that assembly for type resolution instead of creating new non STEs types (VS UI for adding service reference offers similar configuration).
We have a Silverlight 4 client connecting to RIA services. It's based on the standard "Business Application" Silverlight application in VS 2010. The objects we send to the client over RIA services are EF 4 entities that are POCO objects generated from our edmx by T4. Our RIA service is a LinqToEntitiesDomainService<> of our linq repository.
We have two Entities that share similar structure but are unrelated. Both are both have navigation property that represents a single instances of another entity. Both are retrieved using methods marked [Query]. For one entity the navigation property is always correctly populated when we try and access its value; for the other it is never populated, it always remains null (well it seems to hold an EntityRef<> object that points to a null entity). We have verified the relations in the edmx and the generated code on the client side, both seem to share the same structure, so we don’t understand why this difference in behavior is happening. Can anyone shed some light on the subject?
Many thanks,
Robert
Are you using the "[Include]" attribute and "Include" functions in one of your entities / domain services methods and not the other one? If so, this answer to a similar question might help you: If you create a DomainService, exposing an entity, can you access aggregate entities?
Cannot give any definite answer, but you could try by checking these things:
Is there any data in the database for the object that is null
Are the query parameters correct
Do you have a test on the server side to see that the data is available
Is there an attribute missing on one of the objects
I've been using the entity framework in combination with the self tracking entity code generation templates for my latest silverlight to WCF application. It's the first time I've used the entity framework in a real project and my hope was that I would save myself a lot of time and effort by being able to automatically update the whole data access layer of my project when my database schema changed.
Happily I've found that to be the case, updating my database schema by adding a new table, changing column names, adding new columns etc. etc. can be propagated to my business object classes by using the update from database option on the entity framework model.
Where I'm hurting is the CRUD operations within my WCF service in response to actions on my Silverlight client. I use the same self tracking entity framework business objects in my Silverlight app but I find I'm continually having to fight against problems such as foreign key associations not being handled correctly when updating an object or the change tracker getting confused about the state of an object at the Silverlight end and the data access operation within the WCF layer throwing a wobbly.
It's got to a point where I have now spent more time dealing with this quirks than I have on my previous project where I used Linq-to-SQL as the starting point for rolling my own business objects.
Is it just me being hopeless or is the self tracking entities approach something that should be avoided until it's more mature?
What version of self tracking entities are you using?
I'm using the .Net 4.0 version together with visual studio 2010. All CRUD operations work fine, also operation with FK.
I had problems in VS 2008 with FK but that's gone in VS 2010 with .Net 4.0.
If you want, I can provide you some samples.
Greetings
Since STE entity does not support lazy loading you should use Include on the server side include related properties. There is no way to include all related navigation properties. You have to explicitly include the properties. for instance
//server side
customer.Include("Orders.OrderDetails").Include("Address")