How can you make an association without using all entity keys in entity framework? - entity-framework

I'm getting more and more frustrated with EF...
I have a table called ExtendedField with:
Record
DocRef
DocType
Name
Record is the primary key
DocRef and DocType are foreign keys used to identify which Ticket they belong to
Name is the key used by the "definition" table to define what the field actually is
So basically I need 2 associations:
One between Ticket and ExtendedField
on ExtendedField.DocRef=ticket.record
and
ExtendedField.docType=HeaderDocType
One between Definition on
ExtendedField.Name=Definition.FieldName
Then I still need Record to be the primary key so I can directly access the fields.
As near as I can tell this is impossible to do in Entity Framework. For every association all the keys need to be mapped together, whereas I need two keys for one association, 1 key for another one and the actual primary key wouldn't be used in any associations.
It doesn't appear that you can define an association between fields that aren't entity keys either.
So is there any way to do this? Am I missing something?

It's a v1, bro. I myself have had some major pain with mapping of key constraints in EF. I hear that better things are coming in v2.

Related

How to get foreign key value from not fetched relationship?

Having two entities defining relationship by #ManyToOne and #OneToMany, how can I get foreign key without asking from related object and just by looking at defining tables? How do I get OWNER_ID from Owned by something like owned.getOwnerId() instead of owned.getOwner().getId() and still be able to owned.getOwner()?
Map the field in your entity as a basic mapping allows you to use the foreign key directly. You can keep the object reference mapping as well, but one of the two mappings must then be marked as insertable=false, updatable=false so that JPA knows which mapping controls the field in the event they show different values.

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.

EF Error - Unable to update the EntitySet because it has a DefiningQuery

I have removed the PrimaryKey from my table, refreshed the EDMX, and now I am getting this error message when doing db.SaveChanges():
Unable to update the EntitySet 'Results' because it has a DefiningQuery and no element exists in the element to support the current operation.
Now...
1 - I dont want PrimaryKey in my table. The table is just a bag of values, no PK is required.
2 - I read another post where someone suggested to remove DefiningQuery element from EF generated EDMX. It is not working, and I avoid manual changes to automatically generated EDMX.
Any idea how I can avoid this error, and not define PK in my table?
Thanks.
In Entity Framework everything must have a key, at least in the model. You can remove it from the database but still you'll have to define a key in the model, which may be a composite key to ensure it is unique. Otherwise EF won't be able to materialize or save objects correctly.
However, I would use a primary key in the table anyway. It is no trouble at all to have an identity field in it and you don't have to worry about finding a unique combination of fields yourself. I can't imagine that the table does not need some notion of identity, or can it really have two exactly identical rows?

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.

Linq Mapping Problem with 1 to 0.1 relationships

I have an linq to entity mapping issue. I have three tables.
Table 1 - ItemsB(ID(key), Part_Number(will be null until built), other item b information)
Table 2 - ItemsA(ID(key), Part_Number(will be null until built), other item a information)
Table 3 - WebItems(Item_id, web item information) *Consists of items from both ItemsB and ItemsA after they are built and pushed over to this table.
ItemsA/ItemsB will have a 1 to 0.1 relationship with WebItems. Part_Number maps to Item_id.
I am using EF4.0.
Problem is after i set up the association/mappings as stated above i get an error message stating: "Problem in mapping fragment at lines so and so: Column [Part_Number] are being mapped in both fragments to different conceptual side properties."
Usually i know what to do in this case. Get rid of the property [Part_Number]. Problem is i need to access [Part_Number] in both ItemsB and ItemsA quite frequently without going to webitems. Not to mention webitems will not always have the [Part_Number] populated at certain points depending upon whether the items have be pushed to webitems.
Does anyone know how to get around this?
Thanks in advance.
As I understand it ItemA and ItemB to WebItem in one-to-one relation. One-to-one relation in EF always demands that relation is build on primary keys and one side is mandatory (principal) because dependent entity will have primary key and foreign key in one column. Once you assign primary key in dependent entity you must have principal entity with the same primary key otherwise you will violate referential integrity defined by foreign key.
The problem is that your Part_Number is primary key and foreign key in the same time. To allow such mapping in EFv4 you must use Foreign Key association instead of Independent association. Here is brief description how to create foreign key association in the designer. Once you define referential constrain get back to mapping window of association and delete the mapping.