I'm attempting to use Bounded (Db) Contexts in Entity Framework 5.0, and I'm having problems excluding a property from one of the classes included in a specific context. Here is the information (I'll shorten for brevity)
BaseContext.cs
public class BaseContext<TContext> : DbContext where TContext : DbContext
{
static BaseContext()
{
Database.SetInitializer<TContext>(null);
}
protected BaseContext()
: base("name=Development")
{
}
}
IContext.cs
public interface IContext : IDisposable
{
int SaveChanges();
void SetModified(object entity);
void SetAdded(object entity);
}
ISuiteContext.cs
public interface ISuiteContext : IContext
{
IDbSet<Organizations> Organizations { get; set; }
...
}
SuiteContext.cs
public class SuiteContext : BaseContext<SuiteContext>, ISuiteContext
{
public IDbSet<Organizations> Organizations { get; set; }
...
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new OrganizationsConfiguration());
...
...
modelBuilder.Ignore<Colors>();
}
}
Organizations.cs:
public class Organizations
{
public Organizations()
{
Colors = new List<Colors>();
...
...
}
public int Organization_ID { get; set; }
public string Name { get; set; }
public string Address_1 { get; set; }
public string Address_2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public ICollection<Colors> Colors { get; set; }
...
...
}
OrganizationConfiguration.cs:
public class OrganizationsConfiguration : EntityTypeConfiguration<Organizations>
{
public OrganizationsConfiguration()
{
ToTable("Organizations");
HasKey(o => o.Organization_ID);
...
...
HasMany(o => o.Colors);
}
}
How I'm Using (Web API Test Proj)
public class ValuesController : ApiController
{
private readonly IUnitOfWork _uow;
private readonly IOrganizationRepository _orgRepo;
public ValuesController()
{
_uow = new UnitOfWork<SuiteContext>(new SuiteContext());
_orgRepo = new OrganizationRepository(_uow);
}
// GET api/values
public IEnumerable<Organizations> Get()
{
return _orgRepo.RetrieveAll().AsEnumerable();
}
}
And the exception that is occurring:
The navigation property 'Colors' is not a declared property type on type 'Organizations'. Verify that it has not been excluded from the model and that it is a valid navigation property
All I want is for the 'SuiteContext' to have access to Organizations, but not the children (Colors, and a few others)
What am I missing? Perhaps I'm interpreting the exception incorrectly?
Update #1
I've tried (without success - same exception):
modelBuilder.Entity<Organizations>().Ignore(o =>o.Colors);
modelBuilder.Ignore<Colors>();
Update #2
I appears it has something to do with the OrganizationsConfiguration.cs
class and the HasMany()
methods.
If I take out the HasMany(o => o.Colors)
, then I am able to Ignore the classes/properties the way I intend. However, this also causes the database creation to FAIL on recreation. Damned if I do, damned if I don't!