Hello I'm making a web app for events in my city (university homework)
I don't know why but I'm not getting what I want with EF. An event has schedules, and a user can join one shedule and rate it. I don't know why but my model doesn't work (count users in a schedule with users returns 0) also in the table dbo.UserJoinSchedule there is a column of type date... what I am doing wrong?
public class UserJoinEvent{
public virtual int Id { get; set; }
public virtual int EventId { get; set; }
public virtual Event Event { get; set; }
[Required]
public virtual string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual int ScheduleId { get; set; }
public virtual Schedule Schedule { get; set; }
[Range( 0, DomainConstraints.VoteMax )]
public virtual int? Vote { get; set; }
[MaxLength(DomainConstraints.ReviewMaxLen)]
public virtual string Review { get; set; }
}
public class Schedule{
public virtual int Id { get; set; }
[DataType(DataType.DateTime)]
public virtual DateTime DateTime { get; set; }
public virtual int EventId { get; set; }
public virtual Event Event { get; set; }
public virtual ICollection<UserJoinEvent> UserJoined { get; set; }
}
public class Event{
public virtual int Id { get; set; }
[Required, MinLength(DomainConstraints.EventNameMinLen)]
public virtual string Name { get; set; }
[Required]
public virtual string Description { get; set; }
[Required]
public virtual string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual double Latitude { get; set; }
public virtual double Longitude { get; set; }
public virtual ICollection< Schedule > Schedule { get; set; }
public virtual int EventTypeId { get; set; }
public virtual EventType EventType { get; set; }
public virtual ICollection< UserJoinEvent > UserJoined { get; set; }
}
protected override void OnModelCreating(ModelBuilder builder){
base.OnModelCreating(builder);
builder.Entity< Event >( e => e.HasMany( o => o.Schedule ).WithOne( o => o.Event ) );
builder.Entity< Event >( e => e.HasMany( o => o.UserJoined ).WithOne( o => o.Event ).OnDelete(DeleteBehavior.Restrict) );
builder.Entity< EventType >( e => e.HasMany( o => o.Events ).WithOne( o => o.EventType ) );
builder.Entity< Schedule >( e => e.HasKey( o => new {o.EventId, o.DateTime} ) );
builder.Entity< Schedule >( e => e.HasMany( o => o.UserJoined ).WithOne( o => o.Schedule ).OnDelete(DeleteBehavior.Restrict));
builder.Entity< UserJoinEvent >( e => e.HasKey( o => new {o.EventId, o.ApplicationUserId} ) );
builder.Entity< ApplicationUser >( e => e.HasMany( o => o.UserJoined ).WithOne( o => o.ApplicationUser ).OnDelete(DeleteBehavior.Restrict));
}
public enum Role { User, Mod, Admin }
public class ApplicationUser : IdentityUser{
public virtual Role Role { get; set; }
public virtual ICollection<UserJoinEvent> UserJoined { get; set; }
}
Query:
var @event = await _context.Events.Include( e => e.ApplicationUser ).Include( e => e.EventType ).Include( e => e.Schedule ).ThenInclude( uj => uj.UserJoined )
.Include( e => e.UserJoined )
.SingleOrDefaultAsync(m => m.Id == id);
View:
@foreach( var s in Model.Schedule ) {
<div class="row">
<div class="col-md-5">@s.DateTime</div>
<div class="col-md-5"><b>Partecipanti:</b> @s.UserJoined.Count</div>
</div>
}
I fixed, I am stupid! I wanted a unique key on Schedule not a primary
I solved removing:
builder.Entity< Schedule >( e => e.HasKey( o => new {o.EventId, o.DateTime} ) );
and adding:
builder.Entity< Schedule >( e => e.HasAlternateKey( o => new {o.EventId, o.DateTime} ) );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.