Хочу достать из таблицы (OrganizationGroups) многим ко многим данные, но выбивает ошибку типа - "Invalid column name 'OrganizationGroupId'.\r\nInvalid column name 'OrganizationGroupId'."
Примеры запроса
IList<Group> groups = await _repository
.OrganizationGroupRepository.FindByCondition(e => e.OrganizationId == organizationId)
.Include(e => e.Groups)
.Select(e => e.Group)
.ToListAsync();
IList<OrganizationGroup> organizationGroups = await _repository
.OrganizationGroupRepository.FindByCondition(e => e.OrganizationId == organizationId)
.Include(e => e.Groups)
//.Select(e=>e.Group)
.ToListAsync();
BaseEntity:
public class BaseEntity: IBaseEntity
{
[Column("id")]
public Guid Id { get; set; }
[Column("CreatedBy")]
public Guid CreatedBy { get; set; }
[Column("ModifiedBy")]
public Guid ModifiedBy { get; set; }
[Column("CreatedOn")]
public DateTime CreatedOn { get; set; }
[Column("ModifiedOn")]
public DateTime ModifiedOn { get; set; }
}
Organization:
[Table("Organizations")]
public class Organization: BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("OrganizationCode")]
private int _organizationCode { get; set; }
[NotMapped]
public int OrganizationCode => _organizationCode;
[Column("Name")]
public string Name { get; set; }
}
[Table("Groups")]
public class Group:BaseEntity
{
[Column("Name")]
public string Name { get; set; }
}
[Table("OrganizationGroups")]
public class OrganizationGroup: BaseEntity
{
[Column("GroupId")]
public Guid GroupId { get; set; }
public Group Group { get; set; }
public ICollection<Group> Groups { get; set; }
[Column("OrganizationId")]
public Guid OrganizationId { get; set; }
public Organization Organization { get; set; }
}
Таблица в БД:
OrganizationGroupRepository:
public class OrganizationGroupRepository : BaseRepository<OrganizationGroup, ProjectXContext>
{
public OrganizationGroupRepository(ProjectXContext context) : base(context) { }
}
Context:
public class ProjectXContext : IdentityDbContext<User, Role, Guid>
{
public DbSet<Group> Groups { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<CourseCategory> CourseCategories { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<CustomerGroup> CustomerGroups { get; set; }
public DbSet<CustomerCourse> CustomerCourses { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<FileStorage> FileStorages { get; set; }
public DbSet<Lesson> Lessons { get; set; }
public DbSet<Organization> Organizations { get; set; }
public DbSet<OrganizationGroup> OrganizationGroups { get; set; }
public DbSet<OrganizationCategory> OrganizationCategories { get; set; }
public DbSet<OrganizationCustomer> OrganizationCustomers { get; set; }
public DbSet<Storage> Storages { get; set; }
public DbSet<StorageType> StorageTypes { get; set; }
public DbSet<UserRefreshToken> UserRefreshTokens { get; set; }
public DbSet<YouTubeStorage> YouTubeStorages { get; set; }
public ProjectXContext(DbContextOptions<ProjectXContext> options)
: base(options)
{
// Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>(entity => { entity.ToTable(name: "sys_Users"); });
builder.Entity<Role>(entity => { entity.ToTable(name: "sys_Roles"); });
builder.Entity<IdentityUserRole<Guid>>(entity => { entity.ToTable("sys_UserRoles"); });
builder.Entity<IdentityUserClaim<Guid>>(entity => { entity.ToTable("sys_UserClaims"); });
builder.Entity<IdentityUserLogin<Guid>>(entity => { entity.ToTable("sys_UserLogins"); });
builder.Entity<IdentityUserToken<Guid>>(entity => { entity.ToTable("sys_UserTokens"); });
builder.Entity<IdentityRoleClaim<Guid>>(entity => { entity.ToTable("sys_RoleClaims"); });
}
}
BaseRepository:
public abstract class BaseRepository<TEntity, TDbContext> : IRepository<TEntity> where TEntity : BaseEntity where TDbContext: DbContext
{
protected readonly TDbContext _context;
public DbSet<TEntity> Entities => _context.Set<TEntity>();
public DatabaseFacade Database => _context.Database;
protected BaseRepository(TDbContext context)
{
_context = context;
_context.Database.SetCommandTimeout(new TimeSpan(0,5,0)); // 5 min
}
public async Task AddAsync(TEntity entity)
{
await _context.Set<TEntity>().AddAsync(entity);
await SaveAsync();
}
public async Task AddRangeAsync(IEnumerable<TEntity> entities)
{
await _context.Set<TEntity>().AddRangeAsync(entities);
await SaveAsync();
}
public async Task<bool> ContainsAsync(TEntity entity)
{
return await _context.Set<TEntity>().ContainsAsync(entity);
}
public async Task<IEnumerable<TEntity>> FindByConditionAsync(Expression<Func<TEntity, bool>> expression)
{
return await _context.Set<TEntity>().Where(expression).AsNoTracking().ToListAsync();
}
public IQueryable<TEntity> FindByCondition(Expression<Func<TEntity, bool>> expression)
{
return _context.Set<TEntity>().Where(expression).AsNoTracking();
}
public async Task<IEnumerable<TEntity>> GetAllAsync()
{
return await _context.Set<TEntity>().AsNoTracking().ToListAsync();
}
public async Task<TEntity> GetAsync(Guid id)
{
return await _context.Set<TEntity>().FindAsync(id);
}
public async Task RemoveAsync(TEntity entity)
{
_context.Set<TEntity>().Remove(entity);
await SaveAsync();
}
public async Task RemoveRangeAsync(IEnumerable<TEntity> entities)
{
_context.Set<TEntity>().RemoveRange(entities);
await SaveAsync();
}
public async Task UpdateAsync(TEntity entity)
{
_context.Entry(entity).State = EntityState.Modified;
await SaveAsync();
}
private async Task SaveAsync()
{
await _context.SaveChangesAsync();
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
// _context.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~BaseRepository()
// {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}
При запросе должен получить одну организацию и коллекцию групп.
В чем ошибка или что нужно сделать, чтобы запрос сработал?
Invalid column name 'OrganizationGroupId
видимо вы запрашиваете колонку, которой нет в бдBaseEntity
? Где ещё 2 таблицы?