Может ли DbContext принудительно применять политику фильтрации?

Я хотел бы передать значение ctor контекста DbContext, а затем заставить это значение применять «фильтрацию» для связанных DbSets. Возможно ли это ... или есть лучший подход?

Код может выглядеть так:

class Contact {
  int ContactId { get; set; }
  int CompanyId { get; set; }
  string Name { get; set; }
}

class ContactContext : DbContext {
  public ContactContext(int companyId) {...}
  public DbSet<Contact> Contacts { get; set; }
}

using (var cc = new ContactContext(123)) {
  // Would only return contacts where CompanyId = 123
  var all = (from i in cc.Contacts select i);

  // Would automatically set the CompanyId to 123
  var contact = new Contact { Name = "Doug" };
  cc.Contacts.Add(contact);
  cc.SaveChanges();

  // Would throw custom exception
  contact.CompanyId = 456;
  cc.SaveChanges;
}
31
задан Tsahi Asher 10 June 2014 в 07:08
поделиться