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.
Related
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.
In the database, I have two tables PersonalDetails and Officers. There's one-to-many relationship between them.(One person can hold different positions in the same company at different periods). I'm using Entity Framework code first approach to communicate with the database. So far I had one entity called Officer that mapped to a join of the above two tables, But now that I have to perform CRUD operations on Officer this makes me have separate entities for them. Now the question is can I somehow leave the Officer entity as it is but instead of mapping to a view in the database have it descend from the two entities I will create and possibly be able insert/update it and have the changes reflected on the corresponding entities.
Here are the entities:(I've omitted the C# rules for a class definition for brevity )
PersonDetails{Pin,FirstName,LastName}
Officer{OfficerID,Pin,Position,ValidThru}
I will use these entities when I need to update/insert. But in user administration panel, I need to show combined information about users(or Officers).For instance:
Pin FirstName LastName Position ValidThru
I have the following table hierarchy in my database which i map to an Entity Framework model (Database First) using the Table-Per-Type (TPT) inheritance pattern:
The mapping in the EF model is straightforward: AssetContent is a base abstract class while the other 2 are concrete subclasses.
The AssetContent table participates into a many-to-many relationship with another table which, to keep the picture clear, is omitted.
My question is, how do i build a Linq-to-Entities query to load the related AssetContent table using Include() such that the 2 'sub-tables' are not loaded at all? This is especially important for the DatabaseAssetContent table, whose BinaryContent field may be quite large and of no relevance to the issuer of the query i want to build. As far as i observed, Entity Framework automatically loads the entire hierarchy for a table, whether lazy loading is enabled or not, but i am interested in loading only the rows in the AssetContent table.
Is such a query possible using Linq-to-Entities (for Entity Framework 6)?
Eventually, i moved the AssetContent table's fields (except the Id) into another, new table, called AssetContentWithMetadata, which has a 1-1 relationship to the AssetContent table. This way, the AssetContent table remains indeed a bit awkward, with a single field (the ID), but now i can load the metadata table alone, without burdening it with the contents as well.
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.
Say I have the following tables:
Essence, EssenceSet, and Essence2EssenceSet where Essence2EssenceSet holds just the IDs of the 1st 2 tables to form the M:M relationship.
In EF since Essence2EssenceSet has no other fields it is not exposed in the model. I find this makes it difficult to insert records into this table when I already have the 2 IDs needed to create the record but don't necessarily have the Essence and EssenceSet records loaded (Just their IDs)
Is there a way to tell EF to not model this way and always include the join table? Or am I missing an easier way to create these join table records?
You can create M:N relation in EF without retrieving objects as well:
using (var context = new MyContext())
{
var firstEntity = new FirstEntity { Id = firstId };
var secondEntity = new SecondEntity { Id = secondId; }
context.FirstEntities.Attach(firstEntity);
context.SecondEntities.Attach(secondEntity);
firstEntity.SecondEntities = new HashSet<SecondEntity>();
firstEntity.SecondEntities.Add(secondEntity);
context.SaveChanges();
}
Anyway exposing junction table as entity is possible but you will lose comfort of EF and fallback to SQL like approach:
Delete M:N relation created by designer
Add new entity
Add two columns to the new entity representing foreign keys
Map the new entity to junction table
Add associations to related entities
Set referential constraints for added relations
Entity Framework is an ORM, and as such, when you work with it you aren't suppose to think of the database in terms of tables but instead in terms of objects. You shouldn't be inserting the identity into a table that holds the M2M relationship, but you should be loading one side of the relationship, which should expose a collection of the other side and add it to that collection. For a M2M, you may need to load the other side and do the same.
Also, I believe EF prefers all tables to have a single column PK (I could be wrong on this), but you may need to add a column to the M2M and designate it as a PK.