One-to-one self-relationship and Entity Framework - c#

I would like to have entity which can have a child (one or zero). This child is same type as parent. I am not sure how to set entity framework becouse I would like to have two navigation properties for every entity. One for navigation to child and one for navigation to parent. Basically it is exactly the same structure as doubly linked list.
I think this table structure should be enough:
int | id | PK
int | id_next | FK
text | data
But how can I create navigation properties for next/prev items? I am able to create only navigation property for next item.
Thanks for help.

You can't. The problem here is that a one-to-one relation has a very specific requirement - FK value must be unique in the whole table. Once the uniqueness is not enforced you can add a second entity pointing to the same parent and you have a one-to-many relation.
To enforce this in a self referencing relation like you described in your example you will place an unique index on the id_next and it will work in SQL server. The problem is that entity framework doesn't support unique keys. Because of that entity framework is only able to build one-to-one relations between two different entity types where FK in the dependent the entity type is also its PK (the only way how to force FK to be unique) = both entities has same PK value. This cannot work with a self referencing relation because you cannot have two same PK values in one table.

You can do this in EF4 by specifying a 0..1 -> 0..1 relationship on the entity. Name one of the navigation properties "Previous" and the other "Next". This will create a hidden field on the underlying DB.
I haven't thoroughly tested this approach but it seemed to work when I created the database script.

Research Tree structures in the Entity Framework. You basically want a vertical tree (i.e. one branch). The framework won't enforce only one branch, but you can manage that in your business logic.

Related

Entity Framework 6 does not create the mapping tables

I am using an Entity Framework 6 ,I have some table as a mapping tables in the database - to map many to many tables - does not created as an entity when I create the model. what is the problem will be?
that's the tables:
Prop:
PK-Code
PropCh:
PK-CHID
and the mapping table:
Ch_Prop:
PK-CHID
PK-Code
In your schema, the mapping table has no properties other than the FKs of the related entities, what makes it so to say a pure join table.
For this reason, EF does not creates a class to represent that "model", as entries of the table will be internally represented by the state of the navigation properties among the related models.
In most cases this isnt an issue, as normally entities are pre-loaded before being associated through modifications of their navigation properties.

Entity Framework Navigation Property for a field that can store a foreign key from two different tables

I need a table called Loan.
The columns of this table will be:
Id, Type, BorrowId, Description
The trick here is that Type field will determine whether the borrower is an Employee or a Customer and then the BorrowerId will either be an Id from the Employee table or an Id from the Customer table.
Is this bad design as far as Entity Framework is concerned? The reason I ask is because it seems like I won't be able to create a Borrower Navigation property on the Loan table since the Id can be from two tables.
Does anyone have a solution for this? Like how I can change my data models to work with Navigation properties.
A simple answer to your question is "Yes it's a bad design". Referential Integrity should be strictly enforced and when you remove that ability by alternating the reference you create a window for errors. If you want two options create two columns, and create foreign keys on each to the tables they reference. Then your application will be effectively foolproof. :D

Adding a property to an association

Having two tables Courses and Employees, i have a many to many relationship between the two. EF automatically generates an intermediary table called CoursesEmployees.
I want to add a property IsPassed to that intermediary.
I could do this in MySQL but i won't know where to map it to in my data model. Is it possible? Preferably in the data model because i work model-first.
I use all of the latest versions.
The way to achieve what you want in a Model First approach is to explicitly model the relation table and add your IsPassedproperty to it, as I've done below. There is a 1-to-m relationship between Course and Course_Employee entities as well as a 1-to-n relationship between Employee and Course_Employee entities. The combination of the two one-to-many relationships creates the many-to-many relationship between Course and Employee entities.

unwanted column in entityframework database

I am working on an MVC project in Visual Studio Ultimate 2013 and I have some unwanted columns in some of my tables and I cannot really understand why is it so.
In table Categories there is the column Template_TemplateId which is probably because in the Template entity class there is a ICollection<Category> property.
I want a Template to have many categories, however a one category can be repeatedly used by many Templates so I guess the Template_TemplateId column is something I don't want there.
Can someone explain, why is this so, and what should be a right approach to have the model the way I want it?
Thank you.
Model First:
Be default, EntityFramework creates one-to-many type relationships. When you create those, you need a foreign key in the "one" side of the relationship.
This is the "Template_TemplateID" field. It is the foreign key into the "Templates" category. However, you said you wanted a many-to-many relationship, so you are absolutely right; that field is useless!
You need to specify a cardinality of "*" on both ends of the relationship, then the model will create an intermediate table allowing the many-to-many relationship. The foreign key should go away at this point.
Code First:
Same problem, but the solution is to make a collection of "Template" in categories and and a collection of "Category" in templates, thus creating the many-to-many relationship.
If you want a many-to-many relationship between Templates and Categories create two collections, on on each class - on Template, create a ICollection and on Category, create ICollection.

Forcing a one-or-more in the ICollection<> with Entity Framework Code-First

Using Entity Framework 4.1 Code-First, I have set up a many-to-many relationship using the fluent API. If possible, I would like to force one side of the relationship to have 1 or more of the other, instead of the default 0 or more. Currently, if the tables are A and B, A can have 0 or more B's and B can have 0 or more A's, but I would like to force A to have at least 1 B. Can I do this in the data model or do I have to just put it in the business logic?
Many thanks.
You can't define this restriction in the model. Probably because you can't define a corresponding foreign key constraint in the database. You could introduce an additional required navigation reference from A to B to ensure that A always refers to at least one B. Table A would need a non-nullable foreign key column to the table B then. But you still had to check in business logic that this required reference is also an element in the collection of the many-to-many relationship (which is not enforced in the database: A (Id=1) could refer to B (Id=2) but there is no entry (1,2) in the join table).

Categories