Update a composed entity in greenDao - greendao

Let say i have a business domain in which there are two entities, Survey and Question, in OOP terms, the Survey has QuestionsList, the greenDao generation getQuestions method which returns a list of questions resolving 1:M relation from Survey to Question, but there is no method like setQuestions( questionList) which will take a list of question to update. How can i update the questionList for the Survey entity ?

you can use:
getQuestions().add(Question);
but for setting Question parent you should set ParentId for your Question and then add it to QuestionList of Survey. ParentId is the foreign-key of Question which links a question to a survey.
Remember that you must store Question after these changes.

Related

Prisma aggregate sum relation

I have Quiz model and Question model. The relation is many to many relation. The Question model has point field. How can I sum point field of all question models of each Quiz model to get total points that Quiz model has. I have looked through https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing and found nothing about relation sum. Thank you.

how to create a many to many relationships with core data? Swift, xcode

I have 2 entities.
entity 1 - People
entity 2 - books
People entity has a property which is an array of string names of their favorite books.
I need to create a relationship that somehow maps the favorite book of a person to the corresponding book entity object(s).
I am not sure how to do this.
So far I have started by creating a relationship in core data model for people by setting destination to "books" and then for the books entity creating a relationship by setting destination to "people".
I don't see or understand how this will automatically pick out each of the person's favorite books...at the end of the day they are both seperate objects. How will the people class know that for a specific people instance that this, this and this book are that person's favorite?
Person attribute as array of string names of books -- very bad idea!
You need a to-many relationship with the Book entity. That's it.
Person <------------>> Book
Then, to get an array of book titles for a particular person:
(person.books as! Set<Book>).map { $0.title }
The person can have an additional to-one relationship (e.g. called favoriteBook) to one of the books.

entity framework - one entity property linked to two table columns

We have following table structure for our problem domain -
Questionnaire - QuestionnaireID (Primary Key), QuestionID
Question = QuestionID, Description and other 12 propeties.
QuestionGroup = QuestionGroupID, Description and 5 other properties.
The QuestionID of Questionnaire table is related to both Question and QuestionGroup Table.
Now with entity framework i have common structure
Questionnaire - QuestionnaireID (Primary Key), QuestionID, QuestionDetails (Navigation Property of type QuestionBase).
QuestionBase (Parent class for Question and QuestioGroup)
Question
QuestionGroup
How i can map them together so QuestionDetails Property of Questionnaire will contain value from either Question or QuestionGroup usin Entity Framework Code-First.
There is no option of changing database as it is already exists, using EF new version is not problem.
Is it is possible to do so or not?
Thanks
If you cannot change the database you will not be able to map a QuestionDetails navigation property and related QuestionId foreign key property at all because you cannot have single navigation property targeting two related tables. It will work only if QuestionBase entity has related table as well and if you map it as TPT inheritance. Moreover primary key column in both Question and QuestionGroup tables will have to have the same name.
I doubt that you have referential constraint on your QuestionID in Questionnaire table (satisfying relation with both Question and QuestionGroup). That is the first red flag which should tell you that you will not be able to map it.

Entity Framework STEs and many-To-many associations

I'm fairly new to EF and STE's, but I've stumbled on a painful point recently, and I'm wondering how others are dealing with it...
For example, suppose I have two STE's: Employee and Project. It's a many-to-many relationship. Each entity has a navigation property to the other (i.e. Employee.Projects and Project.Employees).
In my UI, a user can create/edit an Employee and associate it with multiple Projects. When the user is ready to commit, a list of Employees is passed to the server to save. However, if an Employee is not added to the "save list" (i.e. it was discarded), but an association was made to one or more Projects, the ApplyChanges extension method is able to "resurrect" the Employee object because it was "connected" to the object graph via the association to a Project.
My "save" code looks something like this:
public void UpdateEmployees(IEnumerable<Entities.Employee> employees)
{
using (var context = new EmployeeModelContainer(_connectionString))
{
foreach (var employee in employees)
{
context.Employees.ApplyChanges(employee);
}
context.SaveChanges();
}
}
I've been able to avoid this issue to now on other object graphs by using FKs to manipulate associations as described here: http://blogs.msdn.com/b/diego/archive/2010/10/06/self-tracking-entities-applychanges-and-duplicate-entities.aspx
How does one handle this when a many-to-many association and navigation properties are involved?
Thanks.
While this answer's a year late, perhaps it will be of some help to you (or at least someone else)
The simple answer is this: do not allow Entity Framework to infer m:m relationships. Unfortunately, I'm not aware of a way of preventing this, only how to deal with it after the fact.
By default, if I have a schema like this:
Employee EmployeeProject Project
----------- --------------- ----------
EmployeeId ---> EmployeeId |--> ProjectId
Name ProjectId ----- Name
... ...
Entity Framework will see that my EmployeeProject table is a simple association table with no additional information (for example, I might add a Date field to indicate when they joined a project). In such cases, it maps the relationship over an association rather than an entity. This makes for pretty code, as it helps to mitigate the oft-referenced impedence mismatch between a RDBMS and object-oriented development. After all, if I were just modeling these as objects, I'd code it the same way, right?
As you've seen, however, this can cause problems (even without using STE's, which cause even MORE problems with m:m relationships). So, what's a dev to do?
(The following assumes a DATABASE FIRST approach. Anything else and you're on your own)
You have two choices:
Add another column to your association table so that EF thinks it has more meaning and can't map it to an association. This is, of course, bad design, as you presumably don't need that column (otherwise you'd already have it) and you're only adding it because of the particular peculiarities of the ORM you've chosen. So don't.
After your context has been generated, map the association table yourself to an entity that you create by hand. To do that, follow the following steps:
Select the association in the designer and delete it. The designer will inform you that the table in question is no longer mapped and will ask you if you want to remove it from the model. Answer NO
Create a new entity (don't have it create a key property) and map it to your association table in the Mapping Details window
Right-click on your new entity and add an association
Correct the entity and multiplicity values (left side should have your association entity with a multiplicity of *, right should have the other entity with a multiplicity of 1)
Check the option that says "Add foreign key properties to the Entity"
Repeat for the other entity in the association
Fix the property names on the association entity (if desired...not strictly necessary but they're almost certainly wrong) and map them to the appropriate columns in the Mapping Details window
Select all of the scalar properties on your association entity and set them as EntityKey=True in the Properties window
Done!

Entity Framework: check existence of record before inserting a new one

In my web app a user can assign muliple tags to products (like the tagging here on stackoverflow).
There are three tables: products, tags and products_tags to implement a many to many relationship.
My question is, how would you implement this with the Entity Framework (LINQ to SQL):
"Insert only a new tag in the tags table if it doesnt already exist there".
So before the insert i have to check first if a tag exists, whats the best way to accomplish this (best performance) ??
thanks for answers
Simple: The Tag should then be the user assigned key/PK of the entity/table.
If you have troubles synchronizing this with the database, I am sure there's something like (N)Hibernate's merge Method in EntityFramework.