Ciao Sto cercando di configurare il mio framework di entità per una relazione molti a molti tra Utente e Ruolo.Ruoli utente di framework Entity Relazione molti a molti
L'immagine qui sotto mostra ciò che è nel database:
Il modello per Utente:
public class User : IEntity
{
public virtual int UserId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public virtual string UserName { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public virtual string FirstName { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public virtual string LastName { get; set; }
[Column(TypeName = "varchar")]
[StringLength(200)]
public virtual string EmailAddress { get; set; }
public int AreaId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string CreatedByUserName { get; set; }
public DateTime CreatedDateTime { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string LastModifiedByUserName { get; set; }
public DateTime? LastModifiedDateTime { get; set; }
//Navigation properties
//public virtual Role Role { get; set; }
public virtual Area Area { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
modello per il ruolo è:
public class Role : IEntity
{
public int RoleId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public string Name { get; set; }
[Column(TypeName = "varchar")]
[StringLength(1000)]
public string Description { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string CreatedByUserName { get; set; }
public DateTime CreatedDateTime { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string LastModifiedByUserName { get; set; }
public DateTime? LastModifiedDateTime { get; set; }
//Navigation Properties
public ICollection<User> Users { get; set; }
}
UserRole è:
public class UserRole
{
public int UserId { get; set; }
public int RoleId { get; set; }
//Navigation properties
public virtual User User { get; set; }
public virtual Role Role { get; set; }
}
così ho pensato che avevo questo set up bene, ma nel mio codice ho andare qualcosa come:
var roles = from r in user.Roles
select r.Name;
e spara per sé dà errori di:
Server Error in '/' Application.
Invalid object name 'dbo.RoleUser'.
così ho aggiunto il seguente al contesto:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasMany(i => i.Roles)
.WithMany(u => u.Users);
}
Comunque ora sto ricevendo errori di:
Errore server nell'applicazione '/'. Nome colonna non valido 'Role_RoleId'. Nome colonna non valido "User_UserId".
Quindi sicuramente non ho impostato correttamente qui qualcosa. Posso andybody indicarmi la giusta direzione?
E il caso in cui l'entità Ruolo non conosce l'entità Utente? Come scrivere il codice di configurazione? –