using EF 7: 1.0.0-rc1-final,
I'm having trouble creating an EF query properly using the first database approach β using ef scaffolding to create some of the model properties listed in DbContext β since the tables contain a large number of columns, I only need to work a bit for webapi to they were displayed in columns
I have 3 objects, brands, events and sessions.
Brands contain many events and events containing many sessions.
my models:
[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; } }
My 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; } }
I use the project as webapi, when I call Brands, it generates the following 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]
where [t]. The column name [EventId1] is invalid Please note that if I comment on the following code in DbContext, this error will disappear and the request will be generated correctly:
modelBuilder.Entity<Event>() .HasMany(s => s.Sessions) .WithOne(e => e.Event).HasForeignKey(s => s.EventId);
I tried to add the attributes [ForeignKey], [InverseProperty] during "fiddling" - this does not seem to matter
I also tried adding the column name explicitly, as mentioned here
I'm not sure what else to try - does this only start to happen when I determine the relation to sessions in FluidUI - sentences?