忽略OnModelCreating中的属性 [英] Ignore Properties in OnModelCreating

查看:133
本文介绍了忽略OnModelCreating中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Entity Framework 5.0中使用Bounded(Db)上下文,但是在将属性排除在特定上下文所包含的类之一之外时遇到了问题。这是信息(为简洁起见,我会简称)

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

BaseContext.cs

public class BaseContext<TContext> : DbContext where TContext : DbContext
{
    static BaseContext()
    {
        Database.SetInitializer<TContext>(null);
    }

    protected BaseContext()
        : base("name=Development")
    {

    }
}

IContext.cs

IContext.cs

public interface IContext : IDisposable
{
    int SaveChanges();
    void SetModified(object entity);
    void SetAdded(object entity);
}

ISuiteContext.cs

ISuiteContext.cs

public interface ISuiteContext : IContext
{
    IDbSet<Organizations> Organizations { get; set; }
    ...
}

SuiteContext.cs

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:

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:

OrganizationConfiguration.cs:

public class OrganizationsConfiguration : EntityTypeConfiguration<Organizations>
{
    public OrganizationsConfiguration()
    {
        ToTable("Organizations");

        HasKey(o => o.Organization_ID);

        ...
        ...
        HasMany(o => o.Colors);
    }
}

我如何使用(Web API Test Proj)

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();
    }
}

发生的异常:


导航属性 Colors不是
类型 Organizations上的已声明属性类型。验证它是否尚未从模型中排除
,并且它是有效的导航属性

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

我想要的只是 SuiteContext可以访问组织,但不能访问孩子(颜色和其他一些孩子)

All I want is for the 'SuiteContext' to have access to Organizations, but not the children (Colors, and a few others)

我缺少什么?

更新#1 strong>

Update #1

我已经尝试过(没有成功-相同的例外):

I've tried (without success - same exception):

modelBuilder.Entity<Organizations>().Ignore(o =>o.Colors);

modelBuilder.Ignore<Colors>();

更新#2

Update #2

我似乎与 OrganizationsConfiguration.cs 类和 HasMany()有关系code>方法。
如果我删除 HasMany(o => o.Colors),那么我就可以按照自己的意愿忽略类/属性。但是,这也会导致数据库创建在重新创建时失败。

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-1方案中效果很好。 1-Many是问题。

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>>();

这篇关于忽略OnModelCreating中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆