Explicit many to many join table in Entity Framework 4 - c#

By default EF hides a many to many join table that does not contain additional data than the foreign keys to the joined tables.
Is it possible to tell EF (and the designer) to explicitly create the join table and make it usable in code?
Thanks

No EF designer will not add this entity for you. If you want junction table exposed you must manually delete created relation and add junction table's entity and two one-to-many FK relations. Here are related questions with step by step guide:
How to expose the join table in many to many relation
How to get Entity to Table mapping for many to many relations

Related

Migrate One-to-Many to Many-to-Many relationship (EF Core 6.0)

I'm looking for some guidance on migrating a relationship in my database from One-to-Many to Many-to-Many. I believe this issue is occurring because Database.EnsureCreated(); was used initially and there are no previous migrations.
When running Add-Migration RelationshipChange, the migration attempts to create the tables (but they already exist in the database) and fails. I can comment out the existing table creation lines (all except the join table) but then other tables do not use the join table and it does not work correctly.
Is there a way I can migrate the data to use the join table for a many-to-many relationship in my current situation?
Cheers :)
You'll have to do it in two steps. First, make sure your new join table is not part of your DbContext. Then you do an Add-Migration InitialTables (or whatever you want to call it) and then delete all the code in the Up and Down methods where the tables are created. Apply the migration to your database (Update-Database). This will get the migration in the database and store the current state where all the tables exist.
Once you've got the initial migration in the database, add your join table to the DbContext and then run your Add-Migration RelationshipChange. Now you should get the code to create the join table and all the ALTER statements to add the foreign keys. One more Update-Database and you should have everything the way you want it.

Entity Framework 6 - Navigation Property as Table

I have a table that links two other tables together. When I import these tables into the edmx file, EF automatically creates a navigation property between the two tables and does not create a entity table for the association. Which is great and useful; however, when doing some complex joins from other various tables it would be far more effective to be able to join off of the association table to limit the number of trips to the database.
I tired a solution where I would remove a table, add the middle table, and then re-add the other table. This worked; however, when building the project I get an error:
Error 3015: Problem in mapping fragments starting at lines 6123, 6490: Foreign key constraint 'fk_Um' from table UserModules (ModuleId) to table Modules (ModuleId):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.
Is there a way to add in the association table, so I can directly join on it without going through the navigation property?

Entity Framework Composite Key

I have a database with two tables, customer and account. Because multiple customers can exist on multiple accounts this is a many to many design.
This is how I designed it in SQL
This works quite nicely as Entity Framework picks up that its a mapping table and just maps Customer to Account as lists on each and hiding the mapping table. Brilliant!
I would like to extend this further to add preferences to a mapping between a customer and an account like such:
Am I right in assuming this is not possible? I have tried adding it to EF model but instead it brings back the mapping table.
Anyone else had any luck with this?
What you have in your first case is a simple many to many relation table, which in Entity Framework results in the collections of entities on one another.
If you want to have a relation to the Preference from your CustomerAccount relation table, the relation becomes complex and it cannot be depicted in the simple relation lists anymore. You need add an entity for your relation CustomerAccount which will have foreign keys on Customer, Account and Preference.

How mapping many-to-many with additional columes in NHibernate

Please I do not know how to map "projekt_etapa" see on picture. it is Many to many, but I have there some another columes. So I do not know If i have tu use bag or make class for it. thank you so mutch.
This is definitely a duplicate of several other questions on StackOverflow. These are 3 of the many.
nhibernate many-to-many mapping - additional column in the mapping table?
additional fields in NHibernate many-to-many relation tables
Fluent Nhibernate Many-to-Many mapping with extra column
Brought to you by google:
site:StackOverflow.com nhibernate many to many additional columns

Linq to SQL many-to-many relationship without a third class

In my database I have the following tables:
Customers (ID)
Orders (ID)
CustomersOrders (CustomerID, OrderID)
How do I map Customers table to Customers class and Orders table to Orders class without creating a class CustomersOrders?
That depends on which LINQ version you're talking about.
If you're using Entity Framework 4.0 and you have no additional information in the table other than the IDs then what you are asking for should already be generated. I believe the same is true for Entity Framework 1.0.
LINQ to SQL is another story. It never handled Many-to-Many relationships well. You have to allow LINQ to SQL generate the third table and then extend the partial classes by hand in a separate file to mask away that third table. It's ugly but it works. Here's a series of blog posts that detail exactly what needs to be done:
How to implement a many-to-many relationship using Linq to SQL

Categories