entity framework - one entity property linked to two table columns - entity-framework

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.

Related

EF Nullable Foreign Key Relationship

I need some help today related to EF relationship. I have two lookup tables Country and Ethnicity. I want to have nullable foreign keys for both in one my table named Singles, so I defined a relationship in my class like
Single Relation
that generate a table like this, which is good so far
Single Relation Result
But I have other fields like Citizenship and CountryOfBirth which require a foreign key as well from Country table. So, I tried to do the same
Multiple Relation with Same Class
But things getting weird inside sql when table created.
Multiple Relation with Same Class Result
I can understand why it behaves odd but don't know how to make it work. Can you please suggest?
Thanks
You'll need to place your ForeignKey attributes on the navigational properties to point to the nullable ID field (instead of vice-versa), and then use the InverseProperty attribute to properly tell EF exactly what kind of relationship you are trying to accomplish.
This answer will be quite similar to that in this SO question.

Entity Framework Code first mapping without foreign key

I have two tables:
Requirement
ID (int) PK
ClientID (int)
JobNumber (int)
Comment
ID (int) PK
Job_ID (int)
Comment (varchar)
The tables don't have foreign keys and there's no possibility of adding any. I'm trying to map them in EF. I have classes for each and I'm trying to define the relationship in fluent code to map the Comment.Job_ID to the Requirement.JobNumber. A requirement can have many comments. Requirement has a list of Comments and Comment has a Requirement property.
I have this mapping setup:
modelBuilder.Entity<Comment>().HasRequired(c => c.Requirement)
.WithMany(s => s.Comments)
.HasForeignKey(f => f.Job_ID);
I'm stuck trying to get Comment.Job_ID to map to Requirement.JobNumber.
Any help appreciated.
It's not possible. With Entity Framework the entity that the Comment.Requirement navigation property is refering to is generally identified by the (primary) key property in Requirement, i.e. by ID. There is no mapping option to define that the target property is anything else than the key property - like JobNumber or another non-key property.
I could only imagine that you could "fake" the primary key property in the model to be JobNumber instead of ID (given that JobNumber is unique in the Requirement table):
modelBuilder.Entity<Requirement>().HasKey(r => r.JobNumber);
I don't know if that could have other unwished side effects. (For sure it doesn't work if JobNumber is not unique because EF wouldn't allow to have more than one entity with the same key attached to a context and updates/deletes and so on wouldn't find the correct record in the database.) It feels wrong and hacky to me. I honestly wouldn't even try that, live with the fact that you don't have a real foreign key relationship in the database, forget the navigation properties Requirement.Comments and Comment.Requirement and use manual joins in LINQ to relate the table data/entities as I need them in a given situation.

Entity Framework 4 many to many relationship issue

Considering this database:
This is what EF generates:
I think that HeroesItem class, is useless, and there should be a navigation property Items on Hero class and a navigation property Heroes on Item class.
I saw this can be done easily with code first, but how to get it done using database first?
It can be done only if your HeroesItems table does not contain Id column and instead uses IdHero and IdItem as composite primary key. Once you add any additional column to junction table you must map it as entity to have control over that column.

Mapping one to one foreign key relationships in Entity Framework 4.0?

I'm sure I'm missing something very simple, but let's say I have two entities, Employee and EmployeeType.
Employee type would contain values like 'Full time', 'Contractor', 'Intern', etc.
An Employee entity would contain one, and only one EmployeeType value.
So I am designing a new .edmx model using the Model-First approach and generating my actual sql server data schema from the model.
I want to add an integer type foreign key id into my Employee entity, EmployeeTypeId, which will map to the primary key of the EmployeeType entity.
So I've gone ahead and done that in my Employee entity. Where I'm stuck is how, though the Entity Framework designer, to enforce the 1:1 referential constraint on that EmployeeTypeId property? Or does the EF handle that automatically behind the scenes?
thanks in advance,
John
Think I figured out the answer to my own question. In the EF .edmx surface designer, I needed to right click on the scalar property I wanted to set as a foreign key id to the other entity and choose 'Entity Key'.
Once that was done, I could go into the referential constraints dialog box and point my new foreign key property to the other entity.
If you don't explicitly set your foreign key property as 'Entity Key', EF will think you want to point your primary key id to the other table.
cheers
You first create a new association (if you haven't done this already) between the two entities. Just right-click on the edmx designer and choose Add -> Association.
When you click on the association you have just created in the model designer, in the properties window, you can set the End1 Multiplicity and End2 Multiplicity properties to 1. This will ensure that you can set only one relation entity while using the entity framework. This does not get enforced in SQL server by the way, because SQL server does not implicitly support 1:1 relationships.

Entity Framework - Change Relationship Multiplicity

I have a table [User] and another table [Salesperson] in my database. [Salesperson] defines a unique UserID which maps to [User].UserID with a foreign key. When I generate the model with Entity Framework I get a 1-to-Many relationship between [User]-[Salesperson], meaning that each User has a "Collection of Salesperson", but what I want is a 0..1-to-1 relationship where each User has a nullable reference to a "Salesperson".
I tried fiddling around with the XML and changing the association's multiplicity settings, but that only produced build errors. What I am trying to achieve is no different than having a nullable SalespersonID in [User] that references [Salesperson].SalespersonID, but because salespeople only exist for specific users it feels like I'd be muddying up my [User] table structure just to get the relationship to point the right way in Entity Framework.
Is there anything I can do to change the multiplicity of the relationship?
Make the PK of Salesperson itself a FK to User. The EF's GUI designer will then get the cardinality correct, since PKs are unique.