Navigation Properties in Entity Framework - entity-framework-4

I have a question on Entity Framework and here's the background for the same.
I have three entities Person, Office, PersonAddress.Person table has a foreign key to officeid from office table and personaddress table has personid as the foreign key. Personid is the primary key of person table.
When entity for Person is generated we will get two navigation properties one for office and other for personaddress. Is there a way through code i can determine person address has a reference to person id of the person table. So while deleting person entity i can get all navigation properties and determine office should not be deleted but person address should be deleted ?

Related

Core Data-GROUP BY using an attribute of another entity which is forms a relationship with my entity

Suppose I have 2 entities in Core Data as follows:-
Entity Employee:
Attributes of Employee:- name, designation, salary, department
Entity Department
Attributes of Department:- name, departmentRank
Here is the department property of Employee is actually a relationship to an element of type Department. It can be assumed to be a one-to-one relationship for now.
What I want to do is to run a GROUP BY query on entity Employee with the departmentRank attribute of the department property. i.e I want to group by rows of Employee using department.departmentRank
Is this possible? Some code or reference to some article would really be appreciated.

Creating relationship in Core Data to perform deletion

I am new to core data just started learning the new ideas in core data.
I have core data database which has three entity Student,Department and an entity for Mapping Student and department.Let name it as StudentDepartment
Student will have all student details with a primary key studentID
Department will have department details with a primary key departmentID
StudentDepartment will have studentID and DepartmentID as foreign key.
Multiple student can be enrolled in a department and a same student can be enrolled to multiple department.
How to create this schema in core data.
If am deleting a studentID in student table subsequent row should be deleted in StudentDepartment table. Similarly if am deleting departmentID in department table subsequent rows should be deleted in StudentDepartment.How to make this relationship by using core data.
Please provide me a xcmodel.
CoreData isn't a database, it's an object store that happens to (sometimes) be implemented on top of a relational database.
The practical result of that is that you really don't need to explicitly create a separate table for relationship mapping. Instead you create your two entities and then create a relationship between the two. From your description, it sounds like you want a many-to-many relationship between the two. At an implementation level, core data will magically create the needed relationship table.
Additionally, you can establish a delete-rule for each side of the relationship that mandates what to do when an item is deleted. Pin this case, you'll want to set the delete rule for both to nullify, which will break the relationship when either end is deleted.

Why can't order id be placed as a foreign key in the customer table?

"The unique identifier of the entity on the “one side” of the one-to-many relationship is placed as a foreign key in the table representing the entity on the “many side.”
Why can't the primary key of the entity on the "many side" of the one-to-many relationship be placed as a foreign key in the table representing the entity on the "one side" ?
Ex. one-to-many relationship between Customer and Order
A customer can place many orders, but each order can be placed by only one customer.
Why can't order id be placed as a foreign key in the customer table?
Any one entity can have only one value of the foreign key, and therefore can reference only one related entity. The referenced entity is thus on the (a) "one" side of the relationship by definition. The entity bearing the FK can be either the other "one" side of a one:one relationship or the "many" side of a one:many relationship; the difference is whether foreign key values may be duplicated in more than one entity of that type.

How can I make a Many to many relation on same table (many to many Join To Self)

I have a problem in my project.
In one database relations have a join to self with many to many.
What any of people solve this problem in Entity FrameWork?
Any many-to-many relationship should create a new table to represent the pairings.
Example: say you have a table People, and you want to show who gave birthday gifts. A person can give gifts to many friends, and a person may receive gifts from many other people.
CREATE TABLE People (person_id INT PRIMARY KEY);
CREATE TABLE GiftGiving (
from_person_id INT,
to_person_id INT,
PRIMARY KEY (from_person_id, to_person_id),
FOREIGN KEY (from_person_id) REFERENCES People(person_id),
FOREIGN KEY (to_person_id) REFERENCES People(person_id)
);
Re your comment:
For EF implementations, see these related questions:
Many to Many self Join with Entity Framework Code First
Entity Framework many-to-many self-reference
Self-referencing many-to-many recursive relationship code first Entity Framework
I simply put an ICollection to itself. Let EF handle the db layer.
public class Person{
public virtual ICollection OtherPersons {get;set;}
}

Combine two table in one Entities Framework entity with DefiningQuery

I am having difficulties combining two entities into one where one is a DefiningQuery of readonly data.
I have a Person entity and a Company entity the Person entity is related to the Company entity throught the Company.CompanyID to the Person.CompanyID as one-to-many. The data for the Company comes from a different database so its represented as a DefiningQuery in my SSDL with a key. I want to make the fields in Company part of the Person entity by combining the entities.
Error 3024: Problem in mapping
fragments starting at line 445:Must
specify mapping for all key properties
(Person.PersonID) of the EntitySet
Person.
I assume the issue is that the Company entity does not have a PersonID but I don't want to make a Company a DefiningQuery with both PersonID and CompanyID
You have Person and Company in one-to-many relation and because of that you cannot map Person and Company fields into same entity. This type of mapping is called entity splitting and it requires one-to-one relation between tables which can be in EF defined only on shared primary key (because EF doesn't support unique constraints).

Resources