[EF4.3] Code-First ingnorare l'eredità
-
giovedì 23 febbraio 2012 13:31
Salve a tutti,
Sto facendo i primi passi nell'utilizzo dei EF e ho riscontrato un problema. Ho le seguenti classi User ,AutenticatedUser,Document così composte:
public class User : IIdentity { public virtual Guid Id { get; set; } public virtual string Description { get; set; } #region IIdentity Members string IIdentity.AuthenticationType { get { return "Custom"; } } bool IIdentity.IsAuthenticated { get { return false; } } string IIdentity.Name { get { return this.Description; } } #endregion public virtual Role Role { get;protected set; } }
class AutenticatedUser:User,IIdentity { bool IIdentity.IsAuthenticated { get { return true; } } }public class Document { protected Document() { } public virtual Guid Id { get; protected set; } public virtual DateTime? Date { get; set; } public virtual int? Number { get; set; } public virtual DocumentType Type { get; protected set; } public virtual User CreateOf { get; set; } public virtual User UpdateOf { get; set; } public virtual DateTime UpdateAt { get; set; } public virtual Contact Contact { get; set; } // ... public static Document Create(DocumentType type, DateTime? date = null, int? number = null, Contact contact = null) { if (type == null) throw new ArgumentNullException("type"); return new Document() { Id = SequentialGuid.NewGuid(), Type = type, Date = date, Contact = contact, CreateOf = (System.Threading.Thread.CurrentPrincipal.Identity as User)//<-è un AutenticatedUser }; } }Con il seguente codice di mapping:
class UserMap : EntityTypeConfiguration<User> { public UserMap() { this.ToTable("Users"); this.HasKey(t => t.Id) .Property(t => t.Id); this.Property(t => t.Description) .HasMaxLength(30) .IsVariableLength() .IsUnicode() .IsRequired(); this.HasOptional<User,Role>("Role") .WithMany<User,Role>("m_Users"); } }
class DocumentMap : EntityTypeConfiguration<Document> { public DocumentMap() { this.ToTable("Documents"); this.HasKey(t=>t.Id) .Property(t=>t.Id); this.Property(t => t.Date) .IsOptional(); this.Property(t => t.IsSupend); this.Property(t => t.Number) .IsOptional(); this.HasRequired(t => t.CreateOf); this.HasRequired(t => t.UpdateOf); this.Property(t => t.UpdateAt) .IsRequired(); //... this.HasOptional(t=>t.CreateOf); this.HasOptional(t => t.UpdateOf); } }In fase di persistenza di Document viene generata un eccezione perché ovviamente non esiste il mapping di AutenticatedUser. C'è un modo per indicare a EF di considerare AutenticatedUser esattamente come User senza creare TPC/TPH/TPC?
Grazie Anticipatamente a tutti.
Tutte le risposte
-
giovedì 23 febbraio 2012 13:56Ciao, visto che la classe User e AuthenticatedUser sono uguali (stesse proprietà)..mi chiedo come mai non puoi usarne solo una e mappare quella sul db..
Martino Bordin (http://blogs.ugidotnet.org/martinobordin)
-
venerdì 24 febbraio 2012 14:37
Non sono esattamente uguali, come puoi vedere negli snippet postati cambia l'implementazione diIIdentity.IsAuthenticated.
L'istanza di AutenticatedUser è restituita nel momento del login avvenuto con successo ed impostato come identità corrente del custom IPrincipal.

