Using a FK as the descriminator in an inheritance mapping - entity-framework-4

Consider the following schema: Employee, and EmployeeType. Employee has a fk to EmployeeType (EmployeeType might have fields like EmployeeTypeId, Description, etc...).
Now I would like to use EF's inheritance feature to create objects HourlyEmployee, and SaleriedEmployee based on the Employees fk field.
This does not seem possible. Every avenue I have tried has failed. Since Employee's FK is the descriminator for the mapping to the derived entities HourlyEmployee and SaleriedEmployee and the FK to the table EmployeeType.
Is there anything I can do short of adding a seprate field in Employee that is an duplicate of the fk field?

Related

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.

One-to-one relationship with foreign key only in one table

I have 2 entities in my domain that have a one-to-one relationship with one another.
Entity Business has a Promotion. And Promotion has a Business. I know this doesn't make a lot of sense and I could as well integrate the fields of Promotion into my Business entity, but I'm dealing with a legacy database here and I'm trying to model my domain on top of it without changing anything in the database for now.
My problem is that although the relationship goes both ways, it's only stored on the side of the promotion table that has a business_id foreign key, but there is no promotion_id foreign key in the business table. So when I try to run my app, Grails can't find the promotion_id column it expects to find for the promotion field in Business.
Is there any way to model my domain entity so that it understands that the column for the relationship is in the destination table only?
Right after I asked this question, I found my own answer: using hasOne as explained in http://grails.org/doc/latest/ref/Domain%20Classes/hasOne.html

Entity Framework 4: Can't add Referential Constraint to Assiciation with Model First and GUID Ids

I read a lot of examples and tutorials about adding referential constraints but my designer just doesn't give me the FK I would need to select.
I'm using model first and all my IDs are GUIDs.
These are the two entities:
These are the properties of their association:
And this is the "Referential Constraint Dialog":
As you can see: There is no FK to select as "Dependent Property"...
In the database there is a FK column for that:
What am I doing wrong?
Thanx
Because your entity doesn't have any FK property. You must first create the property which will be used as your FK and select it in Dependent Property drop down (it shows only existing properties of dependent entity).

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).

One to One error in Entity Framework 4

I have already read Entity Framework One-To-One Mapping Issues and this is not duplicate as the business rule specs are different here.
There are two tables, Invoices and Orders.
Invoices
-> InvoiceID (Primary, Auto Number)
Orders
-> OrderID (Primary, Auto Number)
-> InvoiceID (FK InvoiceID of Invoices Table)
Now the problem is, EF requires One to Many relationship for this association if names of properties are not same. If names of properties are same then it serves purpose of derived class, but here Order is not derived class or Invoice.
InvoiceID(s) are generated for every shopping cart, but OrderID(s) are only generated for paid invoices, so Every Order has InvoiceID but every Order does not have corresponding Invoice.
If I create a seperate table for this, then I have to write too much code to do it. Is there any way I can remove this restriction and let EF still process my model.
However, currently if I change the model as follow, it works
Invoices
-> InvoiceID (Primary, Auto Number)
Orders
-> OrderID (Auto Number)
-> InvoiceID (Primary, FK InvoiceID of Invoices Table)
But is this good practice? Because by definition InvoiceID of Orders table will certainly be unique, but we will be referring everywhere OrderID for comparison and lot of other references. I know I can index the property, but I dont feel this design is perfect.
What seems to be the obvious solution here is to change the 1:* association between Invoice
and Order in the EDM into a 1:1 association. However, as you experienced, the mapping will not
validate when you have a Foreign Key Association between the two entities as in your model.
The only way to map a unique foreign key association is by using an Independent Association. This is the same type of association that we had in EF3.5, where foreign keys were not supported.
To turn the foreign key association into an independent association would mean removing the InvoiceID foreign key from the Order entity and recreating the association through mappings.
To make the change to the association, you’ll need to do the following:
Delete the InvoiceID foreign key property from Order entity.
Select the Asscoation between Invoice and Order.
In the Properties window for the association, open the Referential Constraints by
clicking the ellipses next to that property.
Delete the constraint by clicking the Delete button.
Right-click the association in the Designer and select Table Mapping from the context menu.
In the Mapping Details window, click the element to expose the drop-down.
From the drop-down, select Order. The mappings should populate automatically.
Return to the Properties window for the association.
For the property called “End2 Multiplicity,” which currently has the value * Collection of Orders, change that property to 1 (One of Order) using its drop-down list.
Validate the model by right-clicking the design surface and choosing Validate. You will see that the error message related to this mapping is gone.
When encountering this problem in your application, you’ll have to decide which is more important to your model and your application logic: the foreign key scalar (e.g., Order.InvoiceID) or being able to define a 1:1 association between one entity (Invoice) and another (Order) when they are joined through a foreign key (InvoiceID).
The good news is that the new EF4.0 Lazy Loading will be still working with Independent Associations, just the Foreign key is not exposed. To get that you would have to go over to the navigation property (Invoice) and read its InvoiceID like the code below:
Order order = context.Orders.First();
int invoiceID = order.Invoice.InvoiceID;
Or you can use the code below to read it right on the Order entity withought having to Lazy Load or Eager Load the Invoice property:
int invoiceID = order.InvoiceReference.EntityKey.EntityKeyValues[0].Value;

Resources