I have read many about how to configure many to many realtionships with EF Code First and fluent configuration. But I couldn't understand why I should create an extra entity for the junction table. Lets assume that I have the following entities:
Department
----------
Id
Name
Material
----------
Id
Name
Now I want to keep records of materials for each department. So I need
DepartmentMaterial
-------------------
DepartmentId
MaterialId
Quantity
What I have read so far I need 3 entities: Department, Material and DepartmentMaterial. So actually I am mapping each table to a corresponding entity which was not I intended to do when I started to learn about DDD. I assumed that EF is going to map that junction table automatically and also queries about material quantities can be done over Department.
Hence, is it possible in EF Code First to configure fluently such a relation without an extra entity?
The junction table can stay out of the conceptual model (class model) if it's only got two foreign keys (DepartmentId, MaterialId). You want to record some data about the association. That's OK, the quantity belongs there. A department has quantity x of material y. Saying that a department has a quantity is meaningless.
This is not against DDD principles. The association class is a first-class citizen in the class model. It could even have its own behavior (like limiting quantities for certain department/material combinations).
This means that your model will not contain a many-to-many association but Department 1-n DepartmentMaterial n-1 Material. And you'll have to query materials of departments through the junction table. This is a very common situation. I hardly ever see pure junction tables in real life. They're collector's items.
Example query:
var query = from d in db.Departments
from dm in d.DepartmentMaterials
select new { Department = d, Materials = dm.Select(x => x.Material)};
Related
In a scenario where we have a many-to-many relationship, what would be the ideal thing to implement an endpoint where you can get the data registered specifically for user X? For example, a class having multiple teachers and a teacher having multiple classes.
I'm using typeorm to make the relations. But on the front end, I have a screen that I need to get all the classes.
That's why I thought of putting the array of classes in the teachers table, because if I implement only the endpoint to get all the classes, I will actually get all the classes, instead of just the classes registered for the user. Or do relationships already solve this problem?
In addition to the specific classes for each teacher, I need to get some data that is in the class. Like name, number of students, etc.
It's quite simple, the best solution would be to put an array of classes in your teacher table. Then they could consume this endpoint to consume the class data.
You need a intermediated table, witch will have the Id of the Teacher (Primary Key of the Teachers table) and the Id of the Class (Primary Key of the Class table).
This way you can know witch or how many class the teacher (ID) have and the same for direction, how many teachers have the class (ID) by query to this table
Example:
Table Teacher:
Id
Name
1
Mary
Table Class
Id
Name
1
Math
2
Geo
Table TeachersClass
Id
TeacherId
ClassId
1
1
1
1
1
2
To know the teacher Mary class:
$ Select * From TeachersClass Where TeacherId = 1
And at the end, with the Class Id you get all information you need from the class table.
I would need some help with a problem we're facing in a company, trying to model every process and entity.
So far we have used an enhanced conceptual model with entities and attributes with relationships but there are some objects that don't exactly match a dimension or a fact table, and this is an entity that can be called "Shops with sales over X units". There is the entity "sales" and "shop" obviously, that would have it's representation in UML as independent entities and represent at the lower level, each sale and shop.
What we need to indicate in UML is an entity that stores the counter of shops with sales over X units, so this has some kind of behavior or conditions.
If we consider the entity, it would need date-from and date-to, and the value (counter), and creating a connection with the shop entity seems enough, but we miss the behavior that expresses "more than x sales". So the behavior could be for example: Go to the shop entity, take the 1st element and navigate to the sales entity, calculating the sales. If it's over X, then value+1, and so on.
I made a simple version of the problem. Blue boxes represent the entities already created, and the orange one is the counter that should count the shops with some constraints.
Is there any way of using some kind of UML diagram that can help us to solve this problem?
You could realize that with an association class:
ShopSales relates Shop and Sales so you can store the number of sales along with other things you might need in that conjunction. The ShopSalesStats could give you the shops by number of sales.
Another (of many) way(s) would be to just hold the count as public property of Shop and let ShopSalesStates handle the counts on all associated Shops.
I want to create a custom entity linked to the contact entity.
The purpose of this new entity is to store competitors of a contact (it is not like the default MCRM competitor entity that as for purpose to store your own competitors).
I tried many ways to do that by creating an entity with single names of competitors & an entity linked to contacts to store competitors names but after, you get the "add a new competitor" & "add an existing competitor" that are refering to the second entity I created: you see the whole list of competitors including duplicates (if many contacts have the same competitor, you see many times the same competitor name).
Is it a relationship problem, or should I use an other way?
Thanks
Did you create a 1 -> Many relationship? E.g. When two contacts share the same competitor you create 2 competitor records.
Try using a Many -> Many relationship, that way two contacts can share the competitor record, e.g. you have 1 competitor record for 2 contact records.
This should reduce duplication.
I've got a data model where there is a Person entity, which has a transformable attribute which is an array of dictionaries containing information. The model is much bigger than that, this is just the part I'm having trouble with. It was designed this way by an old developer, and in taking over the project I need to migrate this to be 100% core data.
So what I need to do is create a new entity, then step through each dictionary in the Person's array and create new instances of that entity with the information from that dictionary. I thought I could use an NSEntityMigrationPolicy to set up a custom migration for this new Entity, but it seems the Core Data migration is expecting X number of source entities to translate to X number of destination entities. Because I technically have 0 source entities right now (because they're in an array that Core Data doesn't really know anything about), I'm not sure how I can make the migration create new entities during the process.
What, or rather where in the migration procedure, is the best way to do what I'm trying to accomplish? I've always used lightweight migration in the past, so this is my first adventure in custom migration.
It would help to have a sense of your data model (schema) - but let's assume that your Person entity now holds home address and list of favorite restaurants. And let's further assume that you will be creating new entities Address and Restaurant along with the following relationships:
Person has one Address, so there's a to-one relationship from Person to Address called "homeAddress". There's an inverse to-many relationship from Address to Person, because many people could live at the same address.
Person has a to-many relationship (called restaurants) to Restaurants. Restaurant could also has a to-many relationship to Person (though this might be one of those cases where bidirectionality doesn't really make sense).
Anyway, the point is that now - in addition to your PersonToPerson NSEntityMigrationPolicy subclass, you will also have PersonToAddress and PersonToRestaurant. These will be the places that you unpack the old data and use it to instantiate and initialize new Address and Restaurant objects.
Of course, there are lots of other complicating issues. For example, you won't want to be creating a new instance of the same Restaurant for every Person who likes it. You will need to keep track of newly created Restaurants.
You will want to order your mappings strategically - probably with PersonToPerson first.
You might want to look at Marcus Zarra's Core Data sample code and maybe even buy his book.
What is an example of an inverse relationship in Apple's Core Data framework?
(Better late than never)
When you have 2 or more entities then you can have a relationship. Say for example , there are 2 entities: Book and Publisher. We have a very simple relationship between them as:
Every book has a publisher and a publisher may publish many book.
(In coredata , relationship is not an entity like in RDBMS. Infact relationship is a part of 1 entity. Relationship between A and B in coredata means , A store the reference of another entity. So, when the managed object is created from the entity A,then relationship will become a property of any object created from entity A.)
In the above example, book to publisher is one-to-one relationship and from publisher-to-book is one-to-many. That means book and publisher has two way relationship no matter it's 1-to-1 or 1-to-many , this bidirectional relation is set to inverse in coredata.This kind of relation is known as inverse relation. If you set the book as a inverse to publisher then automatically publisher becomes inverse of book.
It's not technically essential but highly recommended by apple.If one is changed another is affected. What this let us do is keep the object graph more controlled and consistent.Most relationships are bidirectional like this.
Source: Lynda.com
There's one simple explanation: http://brandontreb.com/core-data-quicktip-inverse-relationships/
Definition from Google:
"in·verse
ˈinvərs,inˈvərs
adjective
1.
opposite or contrary in position, direction, order, or effect.
"the well-observed inverse relationship between disability and social contact"
noun
noun: inverse; plural noun: inverses
1.
something that is the opposite or reverse of something else.
"his approach is the inverse of most research""
What you are looking for from Apple:
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/TroubleshootingCoreData.html#//apple_ref/doc/uid/TP40001075-CH26-SW1
"Core Data uses inverse relationships to maintain referential integrity within the data model. If no inverse relationship exists and an object is deleted, you will be required to clean up that relationship manually."
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/HowManagedObjectsarerelated.html
"Inverse Relationships
Most object relationships are inherently bidirectional. If a Department has a to-many relationship to the Employees who work in a Department, there is an inverse relationship from an Employee to the Department that is to-one. The major exception is a fetched property, which represents a weak one-way relationship—there is no relationship from the destination to the source. See Weak Relationships (Fetched Properties).
It is highly recommended that you model relationships in both directions, and specify the inverse relationships appropriately. Core Data uses this information to ensure the consistency of the object graph if a change is made (see Manipulating Relationships and Object Graph Integrity)."
A great example is the second answer here and it should be upvoted:
Does every Core Data Relationship have to have an Inverse?
Try this (First google result for 'Core Data relationship tutorial iphone') :
http://www.raywenderlich.com/934/core-data-tutorial-getting-started