Sto usando EF
nella mia applicazione ASP.Net Core
e sto cercando di associare una tabella UserNotification
alla mia tabella User
. Queste sono le strutture delle tabelle:
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual UserNotifications { get; set; }
}
public class UserNotifications
{
public int Id { get; set; }
[Key, ForeignKey("User")]
public string UserId { get; set; }
public User User { get; set; }
[Key, ForeignKey("Sender")]
public string SenderId { get; set; }
public virtual User Sender { get; set; }
public string Title { get; set; }
public string Message { get; set; }
}
Quello che ho fatto è creare un ForeignKey
di UserNotifications
che sto per memorizzare tutte le notifiche che l' User
ha ricevuto.
Nella tabella UserNotifications
ho creato due FK
per User
e Sender
. In sostanza, desidero memorizzare l' Id
User
che ha ricevuto la notifica e l' Id
User
che ha inviato la notifica ( Sender
).
All'interno di OnModelCreating
ho anche definito la seguente logica:
builder.Entity<UserNotifications>(entity =>
{
entity.HasKey(n => n.Id);
entity.HasOne(u => u.User)
.WithOne(u => u.UserNotifications)
.HasForeignKey<User>(u => u.Id);
entity.HasOne(u => u.Sender)
.WithOne(u => u.UserNotifications)
.HasForeignKey<User>(u => u.Id);
});
Quando digito il seguente edificio nella console
:
add-migration InitialMigration -context MyAppContext
Ottengo:
Impossibile creare una relazione tra "User.UserNotifications" e "UserNotifications.Sender", poiché esiste già una relazione tra "UserNotifications.User" e "User.UserNotifications". Le proprietà di navigazione possono partecipare solo a una singola relazione.
Sono un principiante di EntityFramework
quindi non so come risolvere questo problema, qualcuno potrebbe spiegare cosa ho sbagliato?
Grazie in anticipo per qualsiasi aiuto.
Il modello che stai descrivendo rappresenta due relazioni uno-a-molti tra le entità User
e UserNotifications
(btw, l'entità dovrebbe essere chiamata UserNotification
). Ogni relazione EF può essere associata a 0 o 1 proprietà di navigazione uniche su ciascun lato.
Hai già due proprietà di navigazione di riferimento User
e Sender
(e FK corrispondenti) in UserNotifications
. Quello di cui hai bisogno sono due proprietà di navigazione raccolta corrispondenti in User
:
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<UserNotifications> ReceivedNotifications { get; set; }
public virtual ICollection<UserNotifications> SentNotifications { get; set; }
}
e mappare l'API con fluente:
builder.Entity<UserNotifications>(entity =>
{
entity.HasKey(n => n.Id);
entity.HasOne(n => u.User)
.WithMany(u => u.ReceivedNotifications)
.HasForeignKey(n => u.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Delete);
entity.HasOne(n => n.Sender)
.WithMany(u => u.SentNotifications)
.HasForeignKey(n => n.SenderId)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
});
Si noti che poiché tale modello introduce i cosiddetti percorsi a cascata multipli , è necessario disattivare almeno uno degli eliminati in cascata e gestirli manualmente.