2

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)、意図したとおりにクラス/プロパティを無視できます。ただし、これにより、データベースの作成が再作成時に失敗することもあります。もし私がそうしなかったらのろわれた!

4

2 に答える 2

1

クラスを無視しているように見えますが、プロパティも無視する必要があります。

Ignore(o => o.Colors);

私は以前にアプリのスカラー プロパティを無視したことがあります (そして、あなたが見たビデオでそれをしました :) ) が、ナビゲーション プロパティは無視したので、モデル ビルダーがそれに満足していることを確認するためにテストしました。そしてそうです。ただし、アプリですべてが期待どおりに機能することを確認するために、いくつかの統合テストを行う必要があります。

于 2012-10-16T14:52:04.030 に答える
1

これは古い投稿であることは知っていますが、おそらくこれを理解していると思いますが、これは他の誰かを助けるかもしれません. はIgnore1 対 1 のシナリオでうまく機能します。1-Many が問題です。

上記の例では、次のことを行う必要があります。

 modelBuilder.Ignore<ICollection<Color>>();
于 2015-01-15T18:18:14.890 に答える