Create New Entity With New Retaled Entities in 1 Transaction - relationship

We are dynamics api to create entities as below:
Guid id = _orgService.Create(entity);
How can I create entity with related entities, if related entities are also new?
Should I create all entities seperately and call
_orgService.Associate(entity.LogicalName, entity.Id, relationship, relatedEntities); ??
Or can I do it with 1 transaction?
Thanks...

Have you looked into creating the related entities via Plugin? You could make one create call to the parent entity and have the plugin that executes create the child or related entities.
If that's not feasible then I think you'll need multiple service calls. You cant add the related entity reference if the related entity doesn't already exist. You'll have an exception returned.
If you go the service call route then the least number of calls you can make is 2. One to create the initial entity, and a second to create the related entity with the relationship populated.

Related

Asp.Net core Deleting an entry from table in database: what is the outcome

I have a database with table Photos and table Categories. Each photo is related to one category by categoryId field.
What happens when I delete one category from Categories table? Will the photos with that category be updated with a null value in the categoryID? Or how will entity-framework react to this change?
Another question can I then reset with a mass-change the values of those categories in the photos table? And how can I do that?
hi if you have created relationship between table(using foreign key) then only the deletion of parent table will affect deletion of child table. if you just created table separately and managing relationship with your code then it will not affect the child table. if you are creating using model first approach in entity framework with specifying relation then relationship will be automatically created in backend.
their are four options available in sql on deletion of parent entity
1)No Action
2)Cascade
3)SET NULL
4)SET Default
to know how it will affect check this article
https://www.mssqltips.com/sqlservertip/2365/sql-server-foreign-key-update-and-delete-rules/
That would depend completely on how the ORM that is used defines the database model.
Assuming you use Entity Framework then you can define exactly how EF should react to that situation. In the DbContext you should find a OnModelCreating method in wich you can specify per table what restrictions you want on the table. There you can also define the behaviour of the OnDelete of a foreign key.
If you are not using EF but have your own or a different ORM then again, it depends on how that ORM is configured.
Simple check if you dont know about the used ORM is this: Does the field in the database have a foreign key and how is that configured? Also, is the field categoryID (as defined in the database) nullable? if so, then it apparently doesnt need the relation and shouldnt result in related deletes.

Multiple objects with same FK in context.entity.local [Entity Framework]

I have some problems regarding entity framework and saving to the db.
As my current program works, it deserializes a json-object which results in a list with objects that matches the database. Each of these objectst looks like this:
Every object is a parent with a relation to one child-object.
Each child object have a relation to one or many parent-objects.
After the deserialization is complete each child-objects is created as new object for every parent (meaning I get several instances of the same object).
When i try to save the objects to the db, this offcourse doesn't work since I'm trying to insert many child-objects whith the same pk. I can clearly see that context.childentity.local contains many objects with the same pk.
Is there any easy way to solve this issue? Can I in some way tell EF to refer all duplicates to the same object?
Best regards Anton

Breezejs, OData can't see navigation property entity in client for many to many relationship

I'm working on many to many relationship for BreezeJS/WebAPI/OData like this:
Metadata:
and get data with dataContext.Users.Include("Customers") is fine:
but why in result user object doesn't have customers navigation property:
I read this article but no luck still can't use user.customers()
I'm totally stuck, please help me.
Thanks in advance.
EntityFramework knows about the UserCustomers table, even though it is not exposed as an entity. But Breeze only knows what is in the metadata, and EF will not generate Breeze metadata for UserCustomers unless it is exposed as an entity. (Often this is what you want anyway, because it allows you to add additional data to the relationship table, such as the date that the UserCustomer relationship was added).
The many-to-many mapping table, UserCustomers, must be exposed as an entity. Then your metadata will include that entity, and your Customer entity will have a UserCustomers collection, and your User entity will have a UserCustomers collection.
On the client, if you want to create a new relationship between a User and Customer, you can add a new UserCustomer entity to related them.

in manytomany association, benefit of creating list of associated items

When we have a Many to Many association between two tables, and we create entity java beans for these tables, what is the benefit to create a collection in each tables to reference the items associated to it.
for example, we have two tables A and B which are associated to each other with Many to Many way, and let AB the link table in database.
public class A {
...
List<B> ListBs; // what is the benefit to create this list, the same question in class B.
...
}
The benefits of it are clear, with JPA there's the choice to have the List loaded with the entity itself instead of having to execute a separate query.
This however can be a bit dangerous if you don't manage the LazyInitialization properly. Not establishing it has the result of the whole List being fetched from DB each time you load an Entity. Also setting it to be lazily initized can bring several problems if you try to recover an item from the List once the Session is closed.
In conclussion, you have to manage this kind of associations carefully. Think about what your application needs to be loaded and fit your model to what you really need. If using lazies take care about realoading the object if previous Session has expired.

Many-to-Many relationship in Zend 2 Framework

I use the Zend 2 Framework to build my web application. I implemented my database table models by this tutorial: http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html
I have a many-to-many relationship between two models in my database. To get data from them I googled and found this link: http://mattmccormick.ca/2010/04/24/how-to-easily-create-models-and-table-relationships-in-zend-framework/
The problem is that all the table models extends from Zend_Db_Table_Abstract in the example. I don't know how to get data from the models.
I have a table containing votings, every voting has a unique hash id. Every voting also has tags. Therefore I defined a table tags with all the tags available and a voting_tag_map where all many-to-many relationships are mapped.
What I have tried so far is the following, that's code from my VotingTable class:
public function getTagsByVoting($votingHash){
$select = $this->tableGateway->getSql()->select();
$select->from(array('v' => 'voting'))
->join('voting_tag_map', 'v.voting_id=voting_tag_map.voting_id')
->join('tags', 'voting_tag_map.tag_id=tags.tag_id');
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
It says then:
Since this object was created with a table and/or schema in the constructor, it is read only.
Thats because of the from() method. If I delete the from() method, it says:
Statement could not be executed
Can anyone help me please?
Since this object was created with a table and/or schema in the constructor, it is read only.
This error is because you are trying to set the table name in the from clause, but it's already been set in the contructor of the TableGateway, and you can't change it once set.
If you really need to do this then you can extens AbstractTableGateway yourself then you won't have to add a string tablename to the contructor, but you don't really need to use an alias on your main table...
The SQL error you get when you comment out the from() method will be due to your referencing the votes table as it's alias 'v' in your join, when you are not using the alias v, try changing it to 'voting.XXX' from 'v.XXX'

Resources