utilizzando EF 7: 1.0.0-rc1-final,
Sto avendo problemi con EF che genera la query correttamente, usando il primo approccio del database - usando ef scaffolding per generare alcune proprietà del modello elencate in DbContext - poiché le tabelle contengono un gran numero di colonne ho bisogno solo di poche per lavorare per il webapi quindi sono mappati in colonne
Ho 3 entità, marchi, eventi e sessioni
Marchi contiene molti eventi ed eventi contiene molte sessioni
i miei modelli:
[Table("tblBranding")]
public class Brand
{
[Key]
[Column("brandingId")]
public int BrandId { get; set; }
[Column("BrandingActive")]
public bool Active { get; set; }
[JsonIgnore]
[Column("DeadBrand")]
public bool DeadBrand { get; set; }
[Column("BrandingSiteTitle")]
public string Name { get; set; }
//navigation properties
public virtual ICollection<Event> Events { get; set; }
}
[Table("tblEvents")]
public class Event
{
public int EventId { get; set; }
[Column("eventActive")]
public bool Active { get; set; }
[Column("eventName")]
public string Name { get; set; }
public DateTime EventCloseDate {get;set;}
public int PaxAllocationLimit { get; set; }
//navigation properties
[JsonIgnore]
[Column("brandingId")]
public virtual int BrandId { get; set; }
[JsonIgnore]
[ForeignKey("BrandId")]
public virtual Brand Brand { get; set; }
public virtual ICollection<Session> Sessions { get; set; }
}
[Table("tblEventsDates")]
public class Session
{
[Column("EventDateID")]
public int SessionId { get; set; }
[Column("EventDateName")]
public string Name { get; set; }
[Column("EventDate")]
public DateTime SessionDate { get; set; }
[Column("EventDateTime")]
public DateTime SessionTime { get; set; }
[Column("EventDateMinutes")]
public decimal? SessionDurationInMinutes { get; set; }
[Column("EventDateArrival")]
public DateTime? ArrivalTime { get; set; }
[Column("EventCapacity")]
public int SessionCapacity { get; set; }
//navigation properties
[JsonIgnore]
public virtual int EventId { get; set; }
[JsonIgnore]
public virtual Event Event { get; set; }
}
Il mio DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>()
.HasOne(e => e.Brand)
.WithMany(b => b.Events).HasForeignKey(e=>e.BrandId);
modelBuilder.Entity<Event>()
.HasMany(s => s.Sessions)
.WithOne(e => e.Event).HasForeignKey(s => s.EventId);
modelBuilder.Entity<Event>(entity=> {
entity.Property(e => e.EventId).HasColumnName("EventID");
entity.HasKey(e => new{ e.EventId, e.EventCloseDate});
entity.HasIndex(e => e.EventId).HasName("For Full Text Indexing").IsUnique();
entity.Property(e => e.Active).HasDefaultValue(false);
entity.Property(e => e.EventCloseDate)
.HasColumnType("datetime")
.HasDefaultValueSql("'1/1/2038'");
entity.Property(e => e.Name).HasMaxLength(1024);
entity.Property(e => e.PaxAllocationLimit).HasDefaultValue(10000);
});
modelBuilder.Entity<Brand>(entity => {
entity.HasKey(e => e.BrandId);
entity.Property(e => e.Active).HasDefaultValue(false);
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(150)
.HasColumnType("varchar");
});
modelBuilder.Entity<Session>(entity => {
entity.HasKey(e => e.SessionId);
entity.Property(e=>e.Name)
.HasMaxLength(250)
.HasColumnType("varchar")
.HasDefaultValue("");
entity.Property(e => e.SessionDurationInMinutes)
.HasColumnType("numeric")
.HasDefaultValue(0m);
});
}
public virtual DbSet<Brand> Brands { get; set; }
public virtual DbSet<Event> Events { get; set; }
public virtual DbSet<Session> Sessions { get; set; }
}
Sto usando il progetto come webapi, quando richiamo Brands, genera il seguente SQL:
SELECT [e].[brandingId], [e].[BrandingActive], [e].[DeadBrand], [e].[BrandingSiteTitle]
FROM [tblBranding] AS [e]
WHERE [e].[BrandingActive] = 1
ORDER BY [e].[BrandingSiteTitle], [e].[brandingId]
Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (75ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [t].[EventId], [t].[EventCloseDate], [t].[eventActive], [t].[brandingId], [t].[EventId1], [t].[eventName], [t].[PaxAllocationLimit]
FROM [tblEvents] AS [t]
INNER JOIN (
SELECT DISTINCT [e].[BrandingSiteTitle], [e].[brandingId]
FROM [tblBranding] AS [e]
WHERE [e].[BrandingActive] = 1
) AS [e] ON [t].[brandingId] = [e].[brandingId]
ORDER BY [e].[BrandingSiteTitle], [e].[brandingId]
dove [t]. [EventId1] nome colonna non è valido nota che Se commento il seguente codice in DbContext: questo errore scompare e la query viene generata correttamente:
modelBuilder.Entity<Event>()
.HasMany(s => s.Sessions)
.WithOne(e => e.Event).HasForeignKey(s => s.EventId);
Ho provato ad aggiungere attributi [ForeignKey], [InverseProperty] mentre "giocherellando" - non sembra fare la differenza
Ho anche provato ad aggiungere il nome della colonna esplicitamente come accennato qui
Non sono sicuro di cos'altro provare: inizia a verificarsi solo quando definisco la relazione con Sessioni in FluidUI: suggerimenti?
Solo per divertimento. prova a creare questo database. Ho rimosso un sacco di "clutter" EF è come ASP.NET MVC basato sul concetto di convenzione sulla configurazione.
Hai effettuato la configurazione fino a 2 volte (attributi e / o FluentApi), mentre in realtà avresti potuto farlo 0 volte.
Ecco alcune regole di base sulla convenzione (le convenzioni non sono case sensitive per trigger).
public Brand Brand { get; set; }
, il nome non è importante se c'è un solo "link". public ICollection<Event> Events { get; set; }
è la strada da percorrere. Si potrebbe chiedere che cosa sia su IEnumerable
o IList
(beh, si pensi in questo modo, IEnumerable
non può fare .Add()
quindi è più o meno di sola lettura. IList
bene fa tutto e di più, e sarebbe una bella misura se non fosse perché fa cose che sono intraducibili per SQL, quindi nel mezzo abbiamo ICollection
. Quando usi la parola chiave virtuale? Bene in EF7 non lo usi, come per abilitare il caricamento pigro, e EF7 non ce l'ha (ancora) e non sappiamo se lo devono aggiungere. Richiesta di funzionalità di Github EF7 priva di lazyload
Perché ho rimosso gli attributi [JsonIgnore]
? Non inviare MAI i modelli di entità al client. Creare un DTO appropriato (popolarmente chiamato modello in ASP.NET MVC)
Ricordati di fare Migration, e (per divertimento) prova prima senza requisiti "hardcoded" in FluentAPI e guarda il codice mig che vedrai PK / FK sono fatti, gli indici e molti altri bob e pin sono aggiunti per te.
public class Brand
{
public int Id { get; set; }
public bool Active { get; set; }
public bool DeadBrand { get; set; }
public string Name { get; set; }
//navigation properties
public ICollection<Event> Events { get; set; }
}
public class Event
{
public int Id { get; set; }
public bool Active { get; set; }
public string Name { get; set; }
public DateTime EventCloseDate {get;set;}
public int PaxAllocationLimit { get; set; }
//navigation properties
public Brand Brand { get; set; }
public ICollection<Session> Sessions { get; set; }
}
public class Session
{
public int Id { get; set; }
public string Name { get; set; }
//Datetime contains date and time
public DateTime Time { get; set; }
//TimeSpan is for duration, allowing access to seconds, minutes, hours etc.
public TimeSpan Duration { get; set; }
public DateTime? ArrivalTime { get; set; }
public int SessionCapacity { get; set; }
//navigation properties
public Event Event { get; set; }
}
Classe di contesto
class DbContex{
public virtual DbSet<Brand> Brands { get; set; }
public virtual DbSet<Event> Events { get; set; }
public virtual DbSet<Session> Sessions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Personally I would not have many requirements in the database unless I
//was completely sure it had to be that way.
//They will ALWAYS bite you in the ass in the long run.
modelBuilder.Entity<Event>(entity=> {
entity.Property(e => e.Active).HasDefaultValue(false);
entity.Property(e => e.EventCloseDate)
.HasColumnType("datetime")
.HasDefaultValueSql("'1/1/2038'");
entity.Property(e => e.Name).HasMaxLength(1024);
entity.Property(e => e.PaxAllocationLimit).HasDefaultValue(10000);
});
modelBuilder.Entity<Brand>(entity => {
entity.Property(e => e.Active).HasDefaultValue(false);
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(150)
.HasColumnType("varchar");
});
modelBuilder.Entity<Session>(entity => {
entity.Property(e=>e.Name)
.HasMaxLength(250)
.HasColumnType("varchar")
.HasDefaultValue("");
entity.Property(e => e.SessionDurationInMinutes)
.HasColumnType("numeric")
.HasDefaultValue(0m);
});
}
}
}
Rispondendo alla mia domanda, sembra che potrebbe esserci un bug in EF 7 1.0.0-RC1
nelle proprietà dell'entità per l'evento in DbContext
modelBuilder.Entity<Event>(entity=> {
entity.Property(e => e.EventId).HasColumnName("EventID");
entity.HasKey(e => new{ e.EventId, e.EventCloseDate});
entity.HasIndex(e => e.EventId).HasName("For Full Text Indexing").IsUnique();
entity.Property(e => e.Active).HasDefaultValue(false);
entity.Property(e => e.EventCloseDate)
.HasColumnType("datetime")
.HasDefaultValueSql("'1/1/2038'");
entity.Property(e => e.Name).HasMaxLength(1024);
entity.Property(e => e.PaxAllocationLimit).HasDefaultValue(10000);
});
Si noti che ha 2 chiavi - che è stato generato da scaffolding, la tabella ha una chiave primaria composita
Tuttavia, per i miei requisiti con l'API, ho solo bisogno di un identificatore principale: la rimozione della chiave composita ha corretto l'errore di generazione della colonna non valido
codice aggiornato:
modelBuilder.Entity<Event>(entity=> {
entity.Property(e => e.EventId).HasColumnName("EventID");
entity.HasKey(e => e.EventId);
entity.HasIndex(e => e.EventId).HasName("For Full Text Indexing").IsUnique();
entity.Property(e => e.Active).HasDefaultValue(false);
entity.Property(e => e.EventCloseDate)
.HasColumnType("datetime")
.HasDefaultValueSql("'1/1/2038'");
entity.Property(e => e.Name).HasMaxLength(1024);
entity.Property(e => e.PaxAllocationLimit).HasDefaultValue(10000);
});