dotnet / dotnet/EntityFramework.Docs

Add data annotation examples, clarify what should be used as a string

Open
#4,532 0 comments 0 reactions 0 assignees View on GitHub
area-relationships
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

## 1. Samples for both modeling approaches
[Section](https://learn.microsoft.com/en-us/ef/core/modeling/relationships/foreign-and-principal-keys) would benefit providing samples both with data annotations and model building API. Currently there is only model building API. Referenced [data annotations](https://learn.microsoft.com/en-us/ef/core/modeling/relationships/mapping-attributes) is a separate page and thus is less useful (context switching and different examples). It would be much more beneficial to have tabbed versions of both examples like it is implemented [here](https://learn.microsoft.com/en-us/ef/core/modeling/keys?tabs=data-annotations).

May be not as fancy as selectable tabs, but mere two paragraphs of code for both approaches one below the other would suffice.

## 2. Sample for composite foreign key
Even on referenced [data annotations](https://learn.microsoft.com/en-us/ef/core/modeling/relationships/mapping-attributes) page there is no info about composite foreign keys there. No one could guess that you can write something like `ForeignKey("id1, id2")`. Also current API is [not consistent](https://github.com/dotnet/efcore/issues/31881#issuecomment-1774176608) between primary key (which has params) and foreign key (which has only one string which one could think is only for one name) which adds additional confusion of thinking you could not have more than one property with data annotations.

## 3. Clarification about names used as strings in keys (primary, foreign, indices, etc.)

Suppose you have a code like this for a renamed column:

```
class Item
{
[Column("ActualNameInDb")]
public string Property {get; set;}
}
```

Currently it’s not obvious what name should one use for mapping attributes. One could ask should it be `Index("Property")` or `Index("ActualNameInDb")`(`Index` is just an example, same holds for `PrimaryKey` and others alike).

I think it would be beneficial to clarify this exact case as a separate example as renaming is very much common.

—-
#### Document Details

⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*

* ID: e180427a-a610-e14f-1131-34d856deb98b
* Version Independent ID: e180427a-a610-e14f-1131-34d856deb98b
* Content: [Foreign and principal keys in relationships - EF Core](https://learn.microsoft.com/en-us/ef/core/modeling/relationships/foreign-and-principal-keys)
* Content Source: [entity-framework/core/modeling/relationships/foreign-and-principal-keys.md](https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/modeling/relationships/foreign-and-principal-keys.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @ajcvickers
* Microsoft Alias: **avickers**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.