Loading a reference property of a reference property inside an include EF6 - entity-framework-6

I have an entity that has a parent. That parent has a reference property. How would I load the reference
property of the parent with the original entity as a starting point?
Something like
Context.Filings.Include(x => x.Parent.Include(y = >y.Petitioner)); //obviously second include doesn't even compile

Context.Filings.Include(x => x.Parent.Petitioner);
Courtesy of #IvanStoev

Related

Aurelia: notification when ANY property is modified

Do you see any way to know when ANY model’s property has been modified through a binding?
I would need something generic because it would be applied to all the forms of the application. This means I cannot just have a 'property’Changed() observable callback for every properties of the models. I’m thinking along the ways of overriding the properties setters created by the binding engine so they can call a single defined callback but I feel like there could be a better way.
I created a aurelia-plugin for this kind of scenario (and more).
Its not exactly what your asking for, but can help you a lot.
because the plugin will create a single property called isDirty that you can observe and fire your code accordingly.
https://github.com/avrahamcool/aleph1-aurelia-utilities
look at the Dirty Tracking a model: section
your model class need to extends the baseClass provided by the plugin.
now you can decorate any properties of your model with the
#dirtyTrack() decorator.
for babel users: the assignment in the declaration will set the
default value for the property. for TS users: you should call the
decorator with a parameter #dirtyTrack(7) someInt: number;
this will set up a isDirty variable in your model. this property will
be automatically updated to with every change to your tracked
properties.
at any point, you can call saveChanges() on your model, to commit the
current changes. or discardChanges() to revert back to the last saved
point. you can call serialize() to get a pojo object from your model,
or deserialize(pojo) to populate your model from a pojo object.
Ok, I ended up just using the binding engine to watch all properties changes. This allowed me to implement my isDirty checks without modifying the existing models...
So the final code looks like this:
Object.getOwnPropertyNames(obj).forEach(p => {
this.subscriptions.push(this.binding.propertyObserver(obj, p)
.subscribe(() => this.updateDirty()));
});
my updateDirty() method is called after every property change and no change was necessary to the model.
If anyone can come up with a better solution, I'm still interested but this fits my needs for the time being.

F# not recognizing members of a record type

I am doing a simple mapping and the compiler doesn't recognize the record's members.
The image shows the type OpeningHours itself is clearly ok, with all 3 properties showing:
But hovering over the marked property shows:
error FS0039: The field, constructor or member 'Day' is not defined.
Namespaces are all referenced and I even assign to the very same properties few lines below without any issue.
OpeningHours is a class, not a record. This is one way to create an instance of the class:
OpeningHours(day = oh.Day, opens = oh.Opens, closes = oh.Closes)
Based on the constructor and properties, it looks like OpeningHours is defined as a class, not a record. The record syntax can't be used with classes, so the solution is either to change OpeningHours to a record, or to instantiate it using its existing constructor.

DbContext vs ObjectContext - navigation property behaviour change

I'm migrating a large project from ObjectContext to DbContext, EF6.1.3. Just run into an issue which will be hard to track down instances of reliably in the source code, and am wondering if there may be an approach whereby I can simulate the ObjectContext behaviour.
Consider two classes, Parent and Child. Parent has zero or more Child objects. At the table level, Child has a ParentID column which is in an FK relationship with the ID column in the Parent object. Here are the two POCO classes I generated to illustrate the issue:
Partial Public Class Parent
Public Property ID As Integer
Public Overridable Property Children As ICollection(Of Child) = New HashSet(Of Child)
End Class
Partial Public Class Child
Public Property ID As Integer
Public Property ParentID As Integer
Public Overridable Property Parent As Parent
End Class
and here is a small program to illustrate the issue:
Sub Main()
Using session As New testEntities
Dim parent = session.Parents.Add(session.Parents.Create)
Dim child = session.Children.Create
parent.Children.Add(child)
Console.WriteLine(child.Parent Is Nothing)
session.SaveChanges()
Console.WriteLine(child.Parent Is Nothing)
End Using
With an ObjectContext implementation, Adding the Child to the Parent will also set the Child's Parent property. With DbContext this doesn't happen until the session is commited.
In the code I am migrating there are several places (that we've found so far) where code will be passed the equivalent of a Child object that has been added to a Parent, then attempt to reference the Parent object through the Child's Parent property. These compile correctly, but the runtime behaviour is "broken" with DbContext.
Finding all such instances where this pattern is used will be costly and it will be very easy to miss cases that will then go on to cause problems at runtime. Can anyone suggest a workaround that will allow the code to work as is? I suppose we could modify the TT file to generate our own class instead of a HashSet for the Children property, implement a constructor that takes a reference to the dependant property, and an Add method that updates the dependent property. Before we go down that route, though, is there anything simpler that we may have missed?
I'm not entirely sure this would work, but I think it is worth a try. First, after you add the parent and children, try calling this on the DbContext:
ChangeTracker.DetectChanges()
If this results in what you want, you might be able to create your own DbSet that will call this automatically whenever Add is called... Even if you have to Shadow the Add method. (Hopefully there is just an event you can handle.)

Check for annotation present on superclass field

I have problems to check if a persistent property in grails has an specific annotation for fields that belong to superclass ... ane then get it's name and value.
I am getting the persistence properties as:
GrailsDomainClassProperty[] persistentProperties = new DefaultGrailsDomainClass(entityClass).getPersistentProperties();
That works great ... but later i found that getDeclaredFields only retrieves the actual class fields (not superclass) and things starts to look not very Groovy.
Is there a prefered Groovy way to do this?
No, you should use this code for all super classes. The same will be for children classes.

Get Model Type from RazorView object

I have a System.Web.Mvc.RazorView object which is strongly typed when in cshtml.
Can I get the model type from an instance of this class?
This is possible.
Call BuildManager.GetCompiledType(view.ViewPath) to get the type generated by compiling the view.
You can find the model type by checking the generic argument of the compiled type's base type (which should be WebViewPage<TModel>)
There's no way to get the model given only an instance of a System.Web.Mvc.RazorView. It's available inside the RenderView method which is passed a ViewContext but from the outside you can't access it. But if you are inside a view you could use the Model property.

Resources