EF Core one-to-many without Foreign Key (FK) - entity-framework

Are there ways to make one-to-many (abstract) relation without using SQL foreign keys?
I know it could be made by joining 2 non-related tables. But is it possible to use EF Core default navigation tools?

I think you want that because you can't add a foreign-key to your entity
So, you can solve that so:
You have a entity
Group
and you want a
List<User>
but your User can't be assigned to one group.
So you create a
GroupUser
entity which has a foreign key for the
List<GroupUser>
and a navigation property to the User
This is called a many-to-many relationship

Related

Self-referencing Many to Many relationship in EF Database first

Is there a way to map multiple foreign keys to the same navigation property of an entity? I have a table with a self-referencing many to many relationship defined by a relationship table.
For example, I have a Person table and I want to define the different related people. I created another table, PersonRelationship, that defines those relationships with 2 foreign keys to the Person table. It doesn't matter if the person is referenced in the first or the second foreign key just that the person is in one. I would like my Person entity to have a list of all the relationships but instead it has two lists of relationships: one in which the person is in the first foreign key and another where it is the second foreign key.
How can I map these two foreign keys so that they map to the same list in the Person entity? (I am using a database first approach)

Entity Framework : junction tables with own primary keys

Ì am currently working on a ASP NET MVC project. We use Entity Framework and follow the Database First approach. The database already exists.
The database has been created using the convention, that every table has a specified single primary key, even if it is a junction table.
Example :
Table User :
UserId (PK);
Username
Table UserRole :
UserRoleId (PK);
UserId (FK);
RoleId (FK)
Table Role :
RoleId (PK);
Rolename
As said, the database already exists and this convention is not discussable.
When I want to create an Entity Data Model in Visual Studio, I also have three Entities. But it would only make sense to have two Entities: User and Role. The UserRole Entity makes no sense.
Is there any possibility I can influence the way that Entity Framework maps my tables, so I can get rid of those relational (useless) entities?
Is there any possibility I can influence the way that Entity Framework
maps my tables, so I can get rid of those relational (useless)
entities?
No, you cannot force EF designer to do that. When using automatic tools you will always end with junction table mapped as a separate entity because it is not considered as junction table any more - it has special data (a separate key) which gives this entity new possibilities (for example relation between two entities can exist multiple times which is not possible with normal junction table).
The only way to avoid this is abandon tooling support and use either code mapping or manually write EDMX file and don't tell EF about that additional key. Instead let EF believe that there are only those two FKs forming composite PK as expected from junction table. Obviously if your database requires those special possibilities allowed by separate PK you cannot do this.

Entity Framework Compound Key (Many-To-Many Relationship with a Payload)

I've got database tables like this:
A person may be a member of many teams. A team may have many members. Each person may have a position (think job title) within the team.
I've tried to set this up with ADO.NET Entity Framework and get errors:
Error 3021: Problem in mapping
fragments starting at line ... Each of
the following columns in table
Membership is mapped to multiple
conceptual side properties:
Membership.PersonId is mapped to
<MembershipPerson.Membership.PersonId,
MembershipPerson.Person.Id>
and
error 3021: Problem in mapping
fragments starting at line ... Each of
the following columns in table
Membership is mapped to multiple
conceptual side properties:
Membership.TeamID is mapped to
<MembershipTeam.Membership.TeamId,
MembershipTeam.Team.Id>
The primary key of my Membership entity is a compound key of two foreign keys. I think that's the problem.
What must I do differently?
This happens if you use independent association on the property which is both part of primary key and foreign key. EFv4 introduced Foreign key associations (the difference is described here) and once you expose foreign key in the entity you must define foreign key association. After defining referential constraints delete mapping of independent association in Mapping details window.

Entity Framework Association with Non Key fields

Is it possible to create associates b/t 2 non-key fields in the Entity Framework?
Example: Take the 2 tables in a legacy application (i.e. keys/structure cannot change)
Order (
OrderId : int : PK
OrderNo : varchar
)
OrderDetails (
DetailRecordId : int : PK
OrderNo : varchar
)
In the Entity Framework, I want to create an association b/t Order and OrderDetails by the OrderNo field, which is not a primary key on either table or a FK relationship in the database.
This seems to me as not only should it be easy to do, but one reasons to use something like EF. However, it seems to only want to allow me to create associations using entity keys.
The Entity Framework allows you to claim that columns are keys and that FK constraints exist where none actually exist in the database.
That is because the SSDL (StorageModel part of the EDMX) can if necessary be manipulated by you and lie about the database.
The EF will then interact with the database as if the keys and foreign keys really do exist.
This should work, but all the normal caveats about referential integrity apply.
See my Entity Framework Tips
Hope this helps.
The problem with using non-key fields to define relationships is that the keys are not guaranteed to be properly navigatable. That could lead to a situation where you have a one to one relationship between two entities where there are more than one possible rows that fufill the relationship.
...when relating data from a database, the relationships should always be based on keys. The keys enforce the referential integrity.
One more workaround:
create view vOrder which will not include PK and create Entity from it.
Set PK in this entity to OrderNo
Now you will be able create association

How to insert many2many table into entity

I have a entity and a many to many table associate with it. Because the many2many table have a nchar field, it can not be mapped to a association in EFv1. Is that possible to create a Entity hold both the original entity and the nchar field? Thanks!
You can map such a table, but not as a many to many association with properties, because associations in the EF don't have properties. Instead, you'll have two entities with one to many associations to a third entity representing the association.