Are Junction Tables Implicitly Hidden in Entity Framework Diagrams? - c#

I have junction tables all over my database so that I can create many to many relationships. In one case, I have tables NotificationFormat and Frequency and connect them with a junction called NotificationFormatFrequency.
I have tried to add the junction table to the list of Entity Types, but I have not found a way. 'Update Model from Database...' does not add the junction table to the list of Entity Types, but I do see it in the Entity Store.
After much trial and failure, I looked through and noticed that none of my purely junction tables show in the diagram, nor are they available in the Entity Types to be added to the Diagram. However, in each case I see an 'Association' between my two tables, and I even see a collection of NotificationFormats connected to a collection of Frequencys.
So, expected behavior for modeling many to many relationships in EF? If so, then perhaps I don't want to clutter my diagram with the junction tables, and so maybe I don't care if there's a way to do it. Thanks in advance.

Yes, EF will create intermediate tables for you. For a given relationship, just have both ends use a cardinality of > 1 (*, 1...*).
You won't see the intermediate tables as types or in the designer, but they will appear in the actual DB.

Related

How to delete data in dependent tables in a .NET application when EF Core is used with no tracking?

I am using EF Core code-first approach and this time I used records instead of classes for the database models. So I had to disable tracking and instead of using navigation properties I defined models for linking tables for many to many relations and used foreign keys for one to many relations.
Since EF Core didn't generate ON DELETE CASCADE statements for foreign keys, I have to find a way to delete data from dependent tables when deleting data from entity tables.
What would be the fastest approach? Since I use repositories, I can update the Delete methods to also call Delete methods for other entities. I can alter the migration code generated by EF to define foreign keys and add ON DELETE CASCADE. I can add the same constraints straight to the database. Somehow I don't like the last two approaches, because it makes creating and adding new entities harder.
If I use the Delete from repository approach, is the following a reasonable idea?
get the name of current entity
get all mapped entities from the context
use reflection to get the members of the entities and see if any
matches "Current entity name" + "Id"
delete all matching records

Entity Framework 6 add existing complex type to key columns

I'm working on a project using EF 6 built using database-first approach. There are several tables that previously had the same 3 properties mapped to a complex type (used in several places throughout code). These properties were designated as "foreign key" columns, although their parent-child relationships were never defined in the database.
Recently, a co-worker went through and formally defined the relationships between the FKs and their parent tables in SQL Server. I am now attempting to update the entity model (update from database), and the new associations did not appear. I have resorted to removing the tables from the model and re-adding them. They now show the proper associations.
However, the mapping to Complex Type is gone. When I try to add it back, the updated mappings destroy the associations on those properties (I get a model error when saving). I am aware that Complex Types do not support associations.
Is it recommended to first remove the associations for the columns prior to re-mapping them to the complex type? Will EF observe the underlying key relationship even though the columns are not visible on the entity?
While I would still like to get some feedback on my questions, here's what I did to solve my issues.
1) Removed the associations generated by EF when deleting and re-adding tables from the database. Note that these relationships are only for the foreign key fields which were to be replaced by the complex type.
2) Followed the MS steps (found here) to refactor the requisite key properties to a complex type
3) Optional: It was necessary for me to edit the EDMX directly as XML in order to remove some lingering associations that could not be repaired with the designer
So far, my model functions as intended. I believe the FK relationships are observed during CRUD operations.

Entity Framework Core mismatch in column name

I am trying to do a many-to-many relationship. I have a Customer that can be connected to several CouponCollections but I don't get it to work. I didn't write the code myself and haven't worked with EF that much earlier.
The current error I have, tells me "Invalid column name 'CustomerId'" in CouponCollection when I try to access it. That table has never had a 'CustomerId' column either in the database or in the object, as far as I know.
Is it possible to see how EF is handling the columns and why it thinks that this column should exist? Maybe resetting it and force it to reevaluate all columns.
On a sidenote I don't get migrations to work either. Maybe the problems are connected, but the database just won't update when I change something in the model.
From the docs
Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.
Emphasis mine
Given your issue, you probably need to create the entity to represent the join table as described above.

ADO .NET Entity Framework inheritance modelling

Hi All
I am havinga problem with Entity framework, basically as you can see in the above diagram class A is an abstract class that has two classes that inherit from it B & C.
Each Class has an ID which is not a requirement but was part of the relational structure of the Database design.
I am trying to generate using the EF tools in VS 2008 a model of my Database and then model in the inheritance. I get the model generated fine but the Foreign Keys are still present in the model diagram. I delete those and replace them with the correct inheritance, I also set the Entity Set Names to be the same.
and this is where my problems begin.
The classes do not seem to be working correctly the associations are not working?
Was the removal of the FK correct or should I have left it in place and used the inheritance as well?
I need classes B and C to inherit A in my business layer.
The model doesn't seem correct and I would be gtreatful for any help in making this model work.
You should not have a (separate) FK to the PK of A.
Inheritance expects the B.Id to be PK and an FK to A.Id.
I think the problem is that you have a separate PK and FK that relates your tables. EF expects that the child tables have a single "id" column, in the child tables, that is both the PK and the FK.
Here is a snapshot of an example of a database that I have (this was generated by EF, but you see what I'm talking about):
You'll see here that Jobs is the base and the other two derive from Jobs. You'll see that the Id field in Jobs is the PK, then in the other two tables the Id field is both a PK as well as a FK back to Jobs.
Like I said, this model was generated by EF (Model first), but I'm pretty sure this is how EF wants to see things, so I don't think your database will fit into EF's inheritance structure.

missing Navigation property in auto-generated entity class

I am moving my first steps in the Entity Framework 4.0, and I am currently facing an annoying issue.
The authentication/authorization process of my application is based on the standard ASP.NET membership provider, in other words the database is the well-known ASPNETDB.MDF. In this database there are - amongst others - the tables aspnet_Users and aspnet_Roles, which are linked together by the table aspnet_UsersInRoles.
I generated a new "ADO.NET Entity Data Model", I selected Generate from Database, I provided all the necessary parameters, and the wizard generated for me the relative .EDMX file. I named this "SecurityModel". In the aspnet_User entity I can see there is a navigation property that should retrieve all linked Roles, and viceversa.
At this point I added a new "Domain Service", in my case SecurityDomainService.
When I was asked, I selected the SecurityModel, and all the tables it contains.
Even in this case the wizard generated the SecurityDomainService for me.
Apparently no problems at all. However, I realized that in the entity aspnet_User I have all navigation properties (Membership, Profile, Applications, etc.) but Roles.
I read somewhere that EntityFramework doesnt handle many-to-many relationships. However I can see in my Entity Data Model that an Association exists between aspnet_Roles and aspnet_Users, and it is based on aspnet_UsersInRoles. I can also see in the Data Model designer the "Roles" navigation property in the User entity.
So, my question is why has not this navigation property been generated?
Thanks in advance for all your help.
Cheers,
G.
The problem here is that the aspnet_UsersInRoles table contains only the primary key fields of the tables in the many to many relationship. Entity Framework 'inlines' this table and does not represent it as an entity. Entity Framework handles this fine - it is RIA services that does not support this type of relationship.
Simply adding one extra field to the table will prevent it from being inlined and result in an aspnet_UsersInRoles being generated. This will be supported within RIA Services.
You will need to be careful modifying the aspnet schema to ensure that you do not break any of the stored procedures etc but the addition of a nullable bit column should not cause too much disruption.

Categories

Resources