Jak zmapować w Entity Framework Core więcej niż jedną relację wiele-do-wielu? Na przykład, żeby użytkownik mógł polubić wiadomość i/lub oznaczyć jako ulubioną?
public class User { public int Id { get; set; } public ICollection LikedPosts { get; set; } public ICollection FavoritedPosts { get; set; } } public class Post { public int Id { get; set; } public ICollection UsersThatLike { get; set; } public ICollection UsersThatFavorite { get; set; } } #aspnet #csharp #dotnet #entityframework
Tam pod spodem nadal jest baza danych, potrzebujesz tabeli pośredniej trzymającej Id Usera i Id Postu plus np. typ połączenia (Like i Favorite) lub bez typu połączenia , ale dwóch tabel. Ja bym pierwsze rozwiązanie wybrał.
@30012016: stwórz klasy PostLike i PostFavorite które mają w sobie navigation properties Posta i Usera public class PostLike { public User User { get; set; } public Post Post { get; set; } }
W klasie User możesz mieć ICollection LikedPosts ICollection FavoritePosts
W klasie Post możesz mieć ICollection Likes ICollection Favorites
Zrobiłem tak jak @djfoxer i @blackphoenix zasugerowali. Join Table z enumem. Wszystko działa jak należy a dzięki "Value Conversions" kod ładnie wygląda. Dziękuję wszystkim za pomoc.
Ostatnio wsiąkłem w temat zarówno UFO jak i UAP i mam do was całkowicie poważne pytanie, mieliście w swoim życiu jakieś doświadczenia z potencjalnymi obiektami pochodzenia pozaziemskiego? #ufo #uap
public class User
{
public int Id { get; set; }
public ICollection LikedPosts { get; set; }
public ICollection FavoritedPosts { get; set; }
}
public class Post
{
public int Id { get; set; }
public ICollection UsersThatLike { get; set; }
public ICollection UsersThatFavorite { get; set; }
}
#aspnet #csharp #dotnet #entityframework
public class PostLike
{
public User User { get; set; }
public Post Post { get; set; }
}
W klasie User możesz mieć
ICollection LikedPosts
ICollection FavoritePosts
W klasie Post możesz mieć
ICollection Likes
ICollection Favorites
tutaj fajnie wytłumaczone