Adding a property to an association - c#

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.

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.

Nested relationships never end entity framework

I have 3 tables: Courses, Departments, and Universities.
When I pull the information from the database (using entity framework), I am attempting to map, for example, a Course to a CourseDTO. I am trying to set it up in such a way that the CourseDTO automatically checks if the relationships exist (is not null), then hook up the relationship in the DTO.
Problem is once I run:
db.Courses.Include(c=>c.Department).Include(c=>c.University)
It maps every nested permutation of these relationships, so my automatic mapping to the DTO just creates a stackoverflow because it goes on forever. (eg. Course maps the Department which maps the Courses in that Department, which maps the Department, etc.)
What is the best solution to avoid this problem?
I would suggest turning off Lazy Loading for EntityFramework via
context.Configuration.LazyLoadingEnabled = false
This will prevent the sub-objects that you did not explicitly include from being expanded by Entity Framework when they are accessed.
Either that or create a custom mapping method to include only the objects you want in your DTO.

Is it possible to have an entity that descends from two different 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

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.

Saving List<KeyValuePair<int,string> to a DB with EF 4.3

I have an information that is related to my logical entity class, that is stored as a List<KeyValuePair<int,string>>.
How do I reflect this in my EF code first entities classes to correctly save this data into a database?
When your list entities can e another object then it can be resolved as one to many relationship. So in database it will be two tables with one two many relationship.
So you can add the child entities to the parent
Car car = new Car();
car.Parts.Add(new Part());
Where car has an Parts navigation property defines one to many parts relationship
Good Luck!

Categories