Reputation: 11
I have just started working with EF and I faced this problem. I have a TeamSeason table with many to many relationship between teams and seasons. I have also implemented GenericRepository to communicate with database. My models look like this:
public class Team : ITeam
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Season> SeasonsPlayed { get; set; }
}
public class Season : ISeason
{
public int Id { get; set; }
[Required]
public string LeagueName { get; set; }
public ICollection<Team> Teams { get; set; }
public void AddTeam(Team team)
{
Teams.Add(team);
}
}
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
protected ApplicationDbContext _context;
public GenericRepository(ApplicationDbContext context)
{
_context = context;
}
public virtual void Add(T t)
{
_context.Set<T>().Add(t);
_context.SaveChanges();
}
public virtual async Task AddAsync(T t)
{
_context.Set<T>().Add(t);
await _context.SaveChangesAsync();
}
public virtual int Count()
{
return _context.Set<T>().Count();
}
public virtual async Task<int> CountAsync()
{
return await _context.Set<T>().CountAsync();
}
public virtual void Delete(T entity)
{
_context.Set<T>().Remove(entity);
_context.SaveChanges();
}
public virtual async Task<int> DeleteAsync(T entity)
{
_context.Set<T>().Remove(entity);
return await _context.SaveChangesAsync();
}
public T Get(int id)
{
return _context.Set<T>().Find(id);
}
public IQueryable<T> GetAll()
{
return _context.Set<T>();
}
public virtual async Task<ICollection<T>> GetAllAsync()
{
return await _context.Set<T>().ToListAsync();
}
public virtual async Task<T> GetAsync(int id)
{
return await _context.Set<T>().FindAsync(id);
}
public void Save()
{
_context.SaveChanges();
}
public async virtual Task<int> SaveAsync()
{
return await _context.SaveChangesAsync();
}
public void Update(T t, object key)
{
if (t == null)
{
return;
}
T exist = _context.Set<T>().Find(key);
if (exist != null)
{
_context.Entry(exist).CurrentValues.SetValues(t);
_context.SaveChanges();
}
}
public virtual async Task UpdateAsync(T t, object key)
{
if (t == null)
{
return;
}
T exist = await _context.Set<T>().FindAsync(key);
if (exist != null)
{
_context.Entry(exist).CurrentValues.SetValues(t);
await _context.SaveChangesAsync();
}
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
_context.Dispose();
}
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
EF generated TeamSeason table from this in which has SeasonID and TeamID as foreign keys.
In my controller I already have Season and Team and I just need to somehow represent relationship between them in my database.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> AddTeam([Bind(Include = "SeasonId,SeasonYear,SeasonName,SelectedTeamId")]
TeamSeasonViewModel teamSeasonVM)
{
Season season = await _seasonService.FindSeasonAsync(teamSeasonVM.SeasonId).ConfigureAwait(false);
Team team = await _teamService.FindTeamAsync(teamSeasonVM.SelectedTeamId.Value).ConfigureAwait(false);
return RedirectToAction("Index");
}
Upvotes: 1
Views: 452
Reputation: 730
On your DBContext you can configure the relations, something like:
modelBuilder.Entity<Season>()
.HasMany(b => b.Team)
.WithMany(c => c.Seasons)
.Map(cs =>
{
cs.MapLeftKey("SeasonId");
cs.MapRightKey("SeasonId");
cs.ToTable("SeasonTeam");
});
Upvotes: 1