how to delete an entity in a one to one relation? - entity-framework-4

I have two tables in my database in a one to one realtionship, is a weak relationship.
Videos(IDVideo, IDType, year, ...)
Serie(IDSerie, IDVideo, ...)
When I try to change the type of the video, if the new type is not a serie, I would like to delete the row in the series table that belongs to this video. So I follow this steps:
Videos myVideoDB = myContext.Videos.Include("Series").Where(v=>v.IDVideo == paramVideo.IDVideo).FirstOrDefault<Videos>();
myContext.Series.Remove(myVideoDB.Series.ElementAt(0));
in this second line I get the following error:
Multiplicity constraint violated. The role 'videos' of the relationship 'xxx' has the multiplicity 1 or 0..1.
Why? I think that when I delete the Serie, the DbContext knows that it must delete the entity from the collection of Videos. Otherwise, I try to delete first the entity from the collection of videos and then delete the serie but the problem is not solved.
Thanks.

Well, the solution is that I need to delete the entity Series from the local in the DBContext:
long idSerie = miVideoDB.Series.ElementAt(0).IDSerie;
myVideoDB.Series.Clear();
miContexto.Series.Local.Remove(miContexto.Series.Local.FirstOrDefault<Series>(s => s.IDSerie == idSerie));
What I need is first get the ID of the serie, then clear the Series collection in my video and finllay delete the serie from the local.

Related

Create doesn't make all nodes and relationships appear

I just downloaded and installed Neo4J. Now I'm working with a simple csv that is looking like that:
So first I'm using this to merge the nodes for that file:
LOAD CSV WITH HEADERS FROM 'file:///Athletes.csv' AS line
MERGE(Rank:rank{rang: line.Rank})
MERGE(Name:name{nom: line.Name})
MERGE(Sport:sport{sport: line.Sport})
MERGE(Nation:nation{pays: line.Nation})
MERGE(Gender: gender{genre: line.Gender})
MERGE(BirthDate:birthDate{dateDeNaissance: line.BirthDate})
MERGE(BirthPlace: birthplace{lieuDeNaissance: line.BirthPlace})
MERGE(Height: height{taille: line.Height})
MERGE(Pay: pay{salaire: line.Pay})
and this to create some constraint for that file:
CREATE CONSTRAINT ON(name:Name) ASSERT name.nom IS UNIQUE
CREATE CONSTRAINT ON(rank:Rank) ASSERT rank.rang IS UNIQUE
Then I want to display to which country the athletes live to. For that I use:
Create(name)-[:WORK_AT]->(nation)
But I have have that appear:
I would like to know why I have that please.
I thank in advance anyone that takes time to help me.
Several issues come to mind:
If your CREATE clause is part of your first query: since the CREATE clause uses the variable names name and nation, and your MERGE clauses use Name and Nation (which have different casing) -- the CREATE clause would just create new nodes instead of using the Name and Nation nodes.
If your CREATE clause is NOT part of your first query: your CREATE clause would just create new nodes (since variable names, even assuming they had the same casing, are local to a query and are not stored in the DB).
Solution: You can add this clause to the end of the first query:
CREATE (Name)-[:WORK_AT]->(Nation)
Yes, Agree with #cybersam, it's the case sensitive issue of 'name' and 'nation' variables.
My suggesttion:
MERGE (Name)-[:WORK_AT]->(Nation)
I see that you're using MERGE for nodes, so just in case any values of Name or Nation duplicated, you should use MERGE instead of CREATE.

Access 2016 - Lookup wizard returning the incorrect value

I used the 'Lookup Wizard' to try and create a drop-down list to show the IDs stored in another table, I need the drop-down to also list the employee name so I know what number to select, all of that works, however, when I select the item I need it returns the employee name, not the number. I am new to Access and I hope there is a simple solution for this.
Set the Column Widths property to: 0;1"
This will hide the first column whilst still retaining the link between primary & foreign keys.
I fixed it! I had to switch the select statement to: SELECT [Employee Info].[Vendor ID], [Employee Info].[Employee Name]

On removing stale relationships

A shopping cart is being logged to Neo4j.
NOTE: Each cart is unique to a visitor and defined by the cart:path (which is actually a cart ID cookie). Each item is a lineitem in the cart. Its unique to the cart and product in the cart (unique key is item.key). Finally, product_id refers to a product.
The cart contains lines which need to be updated even when someone deletes a line or changes quantity.
The set updates values for existing lines, but it wouldn't remove lines that are deleted from the cart when the updated cart json arrives.
Is there a simple way to modify this query to delete removed lines automatically?
UNWIND items as item
MATCH (p:Cart {path:px.upath})
SET p.total_price=cart.total_price
MERGE (i:Item {key:item.key})
SET
i.product_id=item.product_id,
i.quantity=item.quantity,
MERGE (i)-[:LineOf]->(p)
I'm a little puzzled by the query, since it seems like you're merging in items as they're added to the cart, as opposed to matching on existing products. Also, the :Item nodes you're adding seem specific for a single user's transaction (you're setting the quantity based on the input), so it seems unwise to use MERGE here...what if two users are trying to add the same type of item at the same time, but different quantities? One of those transactions will overrule the other...user 1 adds 2 of :Item a, but user 2 adds 10 of item a, which changes user 1's :Cart to now read 10 of :Item a selected (though the :Cart's total hasn't been updated to that...) It seems like the model could use some improvements.
One way you could do this is to set the quantity on the relationship to the cart, instead of on the item.
Something like this:
UNWIND items as item
MATCH (p:Cart {path:px.upath})
SET p.total_price=cart.total_price
MERGE (i:Item {key:item.key})
// doesn't seem the place to set product_id, but leaving it in
SET i.product_id=item.product_id
MERGE (i)-[r:LineOf]->(p)
SET r.quantity=item.quantity
So it seems like this query is for adding items to the cart, and you need a query for deleting items.
This should do the trick:
// assume toDelete is your list of items to delete
WITH toDelete
MATCH (p:Cart {path:px.upath})
SET p.total_price=cart.total_price
MATCH (p)<-[r:LineOf]-(i)
WHERE i.key in toDelete
DELETE r
If you want a single query that will set your items as in your first query, and delete all other lines that are not sent over, then this combined query should work:
UNWIND items as item
MATCH (p:Cart {path:px.upath})
SET p.total_price=cart.total_price
// first delete all items from the cart that aren't listed
OPTIONAL MATCH (p)<-[r:LineOf]-(i)
WHERE NOT i.key in items
DELETE r
// now add/update the rest
MERGE (i:Item {key:item.key})
// doesn't seem the place to set product_id, but leaving it in
SET i.product_id=item.product_id
MERGE (i)-[r:LineOf]->(p)
SET r.quantity=item.quantity

NEO4J WEB ADMIN INTERFACE SOME QUESTIONS

I apologize now for my bad English. I'm Italian
I'm just using Neo4j for the thesis, but I still have doubts about the multi use.
1) I have created from the web interface, two nodes. I realized that Neo4j has given these indices 0 and 1 (for research). Now suppose that I was wrong and I have to delete the node with index 1 .. Once deleted do I create a new one and the system puts index 2.
Practically now the first node with index 0 and the second node with index 2 But I want the second node still has index 1 (basically I want to use the index of the first, as I do?)
2) The same problem with the relationship between two nodes. if I'm wrong to create it, the gate and I create another, I lose the index of the one deleted.
3) If I have to create a relationship between 2 nodes with the double arrow, as I do.
I saw that every arrow must have a label, so if I create a relationship between 1 and 2, and a relationship between 2 and 1, you get the double arrow, but with two labels and does not suit me. Thank you for your help
sorry for my very bad English
You should really try to use your own IDs or unique identifier for your nodes, then you can disregard the internal node IDs all together.
If you begin with this Cypher statement in a new database (you only have to set it once),
CREATE CONSTRAINT ON (node:MyNodeLabel) ASSERT node.myid IS UNIQUE
then you can create nodes and relationship like this,
CREATE (a:MyNodeLabel { myid : 0 })
CREATE (b:MyNodeLabel { myid : 1 })
CREATE (a)-[r:RELTYPE]->(b)
or if you do not write the create statements in the same transaction,
CREATE (:MyNodeLabel { myid : 2 })
CREATE (:MyNodeLabel { myid : 3 })
then later,
MATCH (a:MyNodeLabel { myid : 2 }), (b:MyNodeLabel { myid : 3 })
CREATE (a)-[r:RELTYPE]->(b)
or create two nodes and a relationship at the same time
MERGE (:MyNodeLabel { myid : 4 })-[r:RELTYPE]->(:MyNodeLabel { myid : 5 })
You can of course change MyNodeLabel and myid to any identifier you like.
The problem you have with the relationship labels is purely visual or do I misunderstand you?
You know that you can traverse relationships in any direction so maybe you do not need two relationships?
Here is the documentation for Cypher if you have missed it, http://docs.neo4j.org/chunked/stable/.
ok. sorry. I return at home today.... Another question. when you create a relationship with a label on the arrow. how do sometime in the future to change the label
without delete the relationship? is possible?
Q ok. sorry. I return at home today.... Another question. when you create a relationship with a label on the arrow. how do sometime in the future to change the label without delete the relationship? is possible?
ANS Yes,
You can do that easily you can server for the node and remove label only from the node it will not impact the relation ship , but i will suggest you to assing another one if that was the only label on the node so you can group it properly.
match(n:User{Id:1})
remove n:User set n:DeletedUser
return n

rails combine parameters in controller

Hopefully this is a little clearer. I'm sorry but I'm very new to coding in general. I have multiple tables that I have to query in succession in order to get to the correct array that I need. The following logic for the query is as follows:
this gives me an array based upon the store :id
store = Stores.find(params[:id])
this gives me another array based upon the param .location found in the table store where that value equals the row ID in the table Departments
department = Departments.find(store.location)
I need to preform one last query but in order to do so I need to figure out which day of the meeting is needed. In order to do this I have to create the parameter day_of_meeting found in the table Stores. I try to call it from the array above and create a new variable. In the Table Departments, I there are params such as day_1, day_2 and so on. I need to be able to call something like department.day_1 or department.day_2. Thus, I'm trying to actually create the variable by join the words "department.day_" to the variable store.day_of_meeting which would equal some integer, creating department.day_1...
which_day = ["department.day_", store.day_of_meeting].join("")
This query finds uses the value found from the variable department.day_1 to query table Meeting to find the values in the corresponding row.
meeting = Meeting.find(which_day)
Does this make my problem any clearer to understand?
findmethod can only accept parameters like Meeting.find(1) or Meeting.find("1-xx").
so, what you need is Meeting.find(department.send("day_" + store.day_of_meeting.to_s))
Hope to help!

Resources