Entity Framework 5.0 で Bounded (Db) Contexts を使用しようとしていますが、特定のコンテキストに含まれるクラスの 1 つからプロパティを除外する際に問題が発生しています。ここに情報があります(簡潔にするために短くします)
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);
}
}
使い方 (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();
}
}
そして発生している例外:
ナビゲーション プロパティ 'Colors' は、タイプ 'Organizations' で宣言されたプロパティ タイプではありません。モデルから除外されていないこと、および有効なナビゲーション プロパティであることを確認してください
私が望むのは、「SuiteContext」が組織にアクセスできるようにすることだけですが、子 (色、およびその他のいくつか) にはアクセスできません。
私は何が欠けていますか?おそらく私は例外を間違って解釈していますか?
更新 #1
私は試しました(成功せずに-同じ例外):
modelBuilder.Entity<Organizations>().Ignore(o =>o.Colors);
modelBuilder.Ignore<Colors>();
アップデート #2
OrganizationsConfiguration.cs
クラスとHasMany()
メソッドに関係があるようです。を取り出すとHasMany(o => o.Colors)
、意図したとおりにクラス/プロパティを無視できます。ただし、これにより、データベースの作成が再作成時に失敗することもあります。もし私がそうしなかったらのろわれた!