ho fatto esattamente questo su un numero di progetti
Per esempio ho una relazione uno a molti da ASPNetUsers a Notifiche. Quindi, nella mia classe ApplicationUser IdentityModels.cs all'interno devo
public virtual ICollection<Notification> Notifications { get; set; }
La mia classe Notifiche ha il rovescio
public virtual ApplicationUser ApplicationUser { get; set; }
Per impostazione predefinita EF sarà quindi creare un'eliminazione a cascata dalla notifica alla AspNetUsers che io non voglio - così Ho anche questo nella mia classe Context
modelBuilder.Entity<Notification>()
.HasRequired(n => n.ApplicationUser)
.WithMany(a => a.Notifications)
.HasForeignKey(n => n.ApplicationUserId)
.WillCascadeOnDelete(false);
Basta ricordare la definizione per AspNetUSers è esteso nella classe ApplicationUser all'interno IdentityModels.cs che viene generato per te dai ponteggi degli studi visivi. Poi trattarlo come qualsiasi altra classe/tavolo nella vostra applicazione
AGGIORNAMENTO - qui ci sono esempi di modelli full
public class ApplicationUser : IdentityUser
{
[StringLength(250, ErrorMessage = "About is limited to 250 characters in length.")]
public string About { get; set; }
[StringLength(250, ErrorMessage = "Name is limited to 250 characters in length.", MinimumLength=3)]
public string Name { get; set; }
public DateTime DateRegistered { get; set; }
public string ImageUrl { get; set; }
public virtual ICollection<Notification> Notifications { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class Notification
{
public int ID { get; set; }
public int? CommentId { get; set; }
public string ApplicationUserId { get; set; }
public DateTime DateTime { get; set; }
public bool Viewed { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual Comment Comment { get; set; }
}
}
fonte
2015-04-24 13:37:24
Io parto dal presupposto che si sta utilizzando il codice primo approccio. quello che preferirò è provare a generare un edmx per il database e quindi esaminare le classi generate per la tabella di aspnetuser, si otterrà l'idea. per prima cosa crea una relazione con la tabella personalizzata e a livello di db stesso –
@ meajaygoel posso chiederti qualche tipo di codice per favore? – SergioPetrarca
http://www.itorian.com/2013/11/customizing-users-profile-to-add-new.html –