I'm using EF6 (6.1.3 I think) in a web api application. For some odd reason my child collection navigation will not insert when I insert a parent object something in a disconnected context scenario like below:
var child1 = new Child { /*fill values */ };
var child2 = new Child { /*fill values */ };
var children = new List<Child>();
children.Add(child1);
children.Add(child2);
var parent = new Parent
{
// fill other properties
Children = children
}
dbContext.Parents.Add(parent);
dbContext.SaveChanges();
Can you please tell me what am I doing wrong? I thought adding an object to a context would automatically add all children in the entity graph. Also trying to get this to work as part of a Repository pattern. Super confused about the whole thing.
Related
I have a sorting function which accepts TreeVIew Node as argument and then sorts the node. How do I pass top node to this function?
Here's the code of my Tree View:
<asp:TreeView id="mytv" runat="server"></asp:TreeView>
Here's my sorting function code:
private void(TreeNode node)
{
rest of code here
}
I tried the following but it didn't work.
sort(mytv.TopNode)
And
sort(mytv.Nodes)
Try this:
TreeNode currentNode = treeView.SelectedNode;
while (currentNode.Parent != null)
{
currentNode = currentNode.Parent;
}
You are iterating from some node (does not matter which one) and go up the hierarchy until the Parent of the current node is null, that is, the current node is the Root.
Also, here is the class reference for the TreeView:
TreeView class reference
You will be able to figure out, from the class reference that
sort(mytv.Nodes)
did not work because Nodes is not a node but rather a collection of nodes.
Also, the TreeView does not have a TopNode attribute.
Since the Entity Framework creates proxy instead of providing the "original" entity classes, how do you cast a parent class to a child class?
This does not work "the normal way" because the automatically created proxy classes don't use the inheritance structure of the original entity classes.
Turning off the proxy-creation feature is not an option for me.
Any help is welcome, thanks!
How do you cast a parent class to a child class?
You can only cast a parent to a child if the actual runtime type is a child. That's true for non-proxies and proxies because a proxy of a child derives from a child, hence it is a child. A proxy of a parent is not a child, so you can't cast it to a child, neither can you cast a parent to a child.
For example (using DbContext API):
public class Parent { ... }
public class Child : Parent { ... }
Then both the following casts will work:
Parent parent1 = context.Parents.Create<Child>(); // proxy
Parent parent2 = new Child(); // non-proxy
Child child1 = (Child)parent1; // works
Child child2 = (Child)parent2; // works
And both the following won't work:
Parent parent1 = context.Parents.Create<Parent>(); // proxy
Parent parent2 = new Parent(); // non-proxy
Child child1 = (Child)parent1; // InvalidCastException
Child child2 = (Child)parent2; // InvalidCastException
Both casts work "the normal way".
private function editForm():void {
var event:DepManagementEvent = new DepManagementEvent("Edit Page",true);
var navi:String;
event.navi = deleteDataGrid.selectedItem
dispatchEvent(event);
}
This function is in item renderer, i need the parent datagrid id to be called here...
DataGrid(this.owner) or DataGrid(this.listData.owner) would give you the parent data grid. I don't know the exact reasons, but I heard that the second one is the preferred way of doing it.
Some helpful information here:
http://www.adobe.com/devnet/flex/articles/itemrenderers_pt2_04.html
Does anyone know how to programmatically expand the nodes of an AdvancedDataGrid tree column in Flex? If I was using a tree I would use something like this:
dataGrid.expandItem(treeNodeObject, true);
But I don't seem to have access to this property in the AdvancedDataGrid.
AdvancedDataGrid has an expandItem() method too:
http://livedocs.adobe.com/flex/3/langref/mx/controls/AdvancedDataGrid.html#expandItem()
Copy the sample found at the aforementioned url and call this function:
private function openMe():void
{
var obj:Object = gc.getRoot();
var temp:Object = ListCollectionView(obj).getItemAt(0);
myADG.expandItem(temp,true);
}
You could also open nodes by iterating through the dataProvider using a cursor. Here is how I open all nodes at a specified level:
private var dataCursor:IHierarchicalCollectionViewCursor;
override public function set dataProvider(value:Object):void
{
super.dataProvider = value;
/* The dataProvider property has not been updated at this point, so call
commitProperties() so that the HierarchicalData value is available. */
super.commitProperties();
if (dataProvider is HierarchicalCollectionView)
dataCursor = dataProvider.createCursor();
}
public function setOpenNodes(numLevels:int = 1):void
{
dataCursor.seek(CursorBookmark.FIRST);
while (!dataCursor.afterLast)
{
if (dataCursor.currentDepth < numLevels)
dataProvider.openNode(dataCursor.current);
else
dataProvider.closeNode(dataCursor.current);
dataCursor.moveNext();
}
dataCursor.seek(CursorBookmark.FIRST, verticalScrollPosition);
// Refresh the data provider to properly display the newly opened nodes
dataProvider.refresh();
}
Would like to add here that the AdvancedDataGrid, in spite of having an expandAll() method, has a property called displayItemsExpanded, which set to true will expand all the nodes.
For expanding particular children, the expandChildrenOf() and expandItem() methods can be used, as can be verified from the links given above.
I've reviewed all the documentation and Google results surrounding this and I think I have everything setup correctly. My problem is that the symbol is not appearing in my app. I have a MovieClip symbol that I've embedded to my Flex Component. I need to create a new Image control for each item from my dataProvider and assign this embedded symbol as the Image's source. I thought it was simple but apparently not. Here's a stub of the code:
[Embed(source="../assets/assetLib.swf", symbol="StarMC")]
private var StarClass:Class;
protected function rebuildChildren():void {
iterator.seek( CursorBookmark.FIRST );
while ( !iterator.afterLast ) {
child = new Image();
var asset:MovieClipAsset = new StarClass() as MovieClipAsset;
(child as Image).source = asset;
}
}
I know the child is being created because I can draw a shape and and that appears. Am I doing something wrong? Thank you!
You should be able to simply set child.source to StarClass:
child = new Image();
child.source = StarClass;
See the MovieClipAsset Language Reference for more details:
you rarely need to create MovieClipAsset instances yourself
because image-related properties and
styles can be set to an
image-producing class, and components
will create instances as necessary.
For example, to set the application
background to this animation, you can
simply write the following:
<mx:Application backgroundImage="{backgroundAnimationClass}"/>