Any way to check an object if its breeze entity object? - breeze

Any way to check an object if its breeze entity object ?Simply check could be the considering the existence of "entityAspect" prop of obj but i wonder if there is more elegant way of doing that such instanceof ,typeof or else.
function (obj){//Evaluate the obj is breeze entity

Actually checking for the 'entityAspect' property is the right way to go. Remember that breeze can make use of your own custom entity constructors, that have no breeze semantics at all. In these cases, Breeze either wraps or augments your custom entities constructors ( depending on the modelLibrary registered with breeze), so there is no real way other than by checking for the augmentation to determine if the entity is really a breeze entity.

Related

How to prevent lazy loading of entities that were already manually loaded when navigating a property in EntityFramework

Background:
I am using EF4 and ObjectContext. To optimze retrieval of complex object hierarchies, I manually execute database queries and then use ObjectContext.Translatey<T>(DataReader, entitySetName, mergeOptions.AppendOnly) to turn data rows into entities. I then attach the entities to the ObjectContext with Attach method. This also fixes relations between entities.
The problem:
After everything is loaded and set up I try to navigate from parent entity to a child entity (for example Parent.Childs.First()), but EF hits the database to load the kids, even though all the child entities are already present in the ObjectContext and EntitySet. It looks like the reason for this is that parent.Childs.IsLoaded is set to false which makes EF think that it still needs to load the relation.
Question:
How can I tell EF that EntitySet has already been loaded?
Is there a supported way to set RelatedEnd.IsLoaded to true. I wouldn't like to mess with the calling the RelatedEnd.SetIsLoaded internal method.
I found a smilar question here but it relates to DbContext and has no satifying answer ;-)
Matra
It looks like this was implemented in this change:
http://entityframework.codeplex.com/workitem/269
You can now iterate through your entities and tell them that their child collections are already loaded:
foreach (var entity in loadedEntities)
context.Entry(entity).Collection(a => a.SomeChildCollection).IsLoaded = true;
This will prevent entities in SomeChildCollection from being loaded when they are accessed from entity.
I'm not exactly sure what version of EF this appeared in but I'm guessing 6.0.0.
The only way to solve this is to turn off lazy loading. The question you have found on MSDN asks about DbContext but the answer mentions that there is no way to change the value in underlying libraries - ObjectContext API (= EF4 in your case) is the underlying library. Even in .NET 4.5 (EF5) setting IsLoaded is still not available on public API.

Introduce custom property in OData

In my database User table I have DataTime field called DateDeleted - which is null while user exists and is set to the proper value when user "is deleted".
I wonder if there is a way to introduce IsDeleted property for User entity so that
http://odata/service.svc/Users(1)/IsDeleted
will return true or false depending on whether DateDeleted is set or not
My research in google hasn't got any results and I am almost sure it is not possible to implement through odata. Am I right?
With the built in providers this is not possible on the WCF DS side of things. You might be able to somehow do this on the EF side (expose it as a property of the EF entity), but I'm not sure if that's possible.
On the WCF DS side, you would have to implement a custom provider in order to do this. Which may be quite a lot of work unfortunately. If you're interested see this for starters: http://blogs.msdn.com/b/alexj/archive/2010/01/07/data-service-providers-getting-started.aspx.
What Shawn refers to above is a method on the custom provider interface.
You can specify the value you want by implementing the method DataServiceQueryProvider.GetPropertyValue.
Please find the reference here:
http://msdn.microsoft.com/en-us/library/system.data.services.providers.idataservicequeryprovider.getpropertyvalue.aspx
The method takes two parameters, the entity object (a User instance) and the resource property (in this case "IsDeleted"). You can try to get the property value of "DataDeleted" from the entity object, and return the value of "IsDeleted" as you want.

Entity Framework 4: Encapsulate navigation properties in business objects

I want to encapsulate all EF entities in business objects.
The EF entity "Investment" will have a "BoInvestment" business object that holds the entity internal and routes all properties.
For navigation properties that is a challenge.
Let's say my "Investment" has "Reports" has an EntityCollection which is lazy loaded.
My business object "BoInvestment" would need this "Reports" as "BoReports" since "Report" is also encapsulated in a business object.
If I would just return an "IList" it would mean that all Reports are always loaded into memory. I would loose the EF advantage that EntityCollection is only a Querable until it is materialized.
Any Ideas :)
The idea is using entities directly as business objects instead of wrapping the into a new object layer. It will work much better if you use POCOs instead of Entity objects. If you want to stick with your current architecture check Lazy<T> - msdn. That could be a way to implement your navigation properties on business objects.
Why not use an IQueryable? this way you would still have the advantage of query composition and lazy loading.
Another idea would be to take advantage of the fact that the entity objects generated by EF are in fact partial classes that you can extend to include your BO functionality

EF4: Difference between POCO , Self Tracking Entities , POCO Proxies

Can someone point me the difference between POCO , Self Tracking Entities , POCO Proxies?
Actually, I am working Entity Framework 4.0 and POCO(Repository Pattern) and whenever I do some changes in the POCO and call ObjectContext.Savechanges then it reflects to the DB.
My question is,
How does the Context persist the change to the DB since it is not tracked?
Does the Context generates the tracking info on the fly for POCO?
Sample Code I am using,
IEFRepository<Category> catRepository = new EFRepository<Category>();
Category c = catRepository.FindOne<Category>(x => x.Name == "Paper");
c.Name = "Paper";
catRepository.SaveChanges(System.Data.Objects.SaveOptions.None);
Self tracking entities are not POCOs. On the contrary, they are very much persistence-aware. More so than EntityObject entities, even. What makes them unique is the changes can be tracked even when they are not attached to an ObjectContext.
"Pure" POCOs, as you say, make change tracking difficult. Really, the only thing you can do is compare snapshots of the object. The object context has a DetectChanges method for this.
With a pseudo-POCO proxy, what you really have is a type which looks (almost) like a POCO at compile time and like a non-POCO at runtime. I say "almost" because at runtime you will get an instance which is a subtype of the compile-time type. Because of this, any properties for which you want to track changes must be non-private and virtual. Similar restrictions apply to lazy loading. You can read more about this in this series of articles on the ADO.NET team blog.

Determining which fields have changed on binding

How can I determine which fields have changed after model has been edited
You could implement INotifyPropertyChanged on your entities. You would need fire off the PropertyChanged event for each property...so there is some refactoring you would have to do to get this working. Its the only built-in way to achieve it with .NET.
If you don't want to implement INotifyPropertyChanged manually, you could use PostSharp to update your classes and adjust your properties at compile time. However, this would require a considerably more complicated effort up front.
If you are using Linq to SQL, the GetModifiedMembers method takes an argument of tybe Object, and returns an array of System.Data.Linq.ModifiedMemberInfo objects. Every Table class in the DataContext has a GetModifiedMembers method that can be invoked on any entity.
http://msdn.microsoft.com/en-us/library/system.data.linq.itable.getmodifiedmembers.aspx
In the Entity Framework, using ObjectStateManager, one can access all this change-information like object-state (added/modified/deleted), modified properties, original and current values
IEnumerable<ObjectStateEntry> changes =
this.ObjectStateManager.GetObjectStateEntries(
EntityState.Added | EntityState.Deleted | EntityState.Modified);

Resources