问题
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!
回答1:
It looks like you're ignoring the class but you also need to ignore the property.
Ignore(o => o.Colors);
I've ignored scalar properties in apps before (and did that in the video you saw :) ) but not navigation properties, so I just tested to ensure that the model builder is happy with it. And it is. But you should just do a few integration tests to make sure it all works as expected in your app.
回答2:
I know this is an old post and you probably have figured this out, though this may help someone else. The Ignore
works well in a 1-1 scenario. The 1-Many is the issue.
In your example above, you would need to do the following:
modelBuilder.Ignore<ICollection<Color>>();
来源:https://stackoverflow.com/questions/12917006/ignore-properties-in-onmodelcreating