我想将值传递给DbContext的ctor,然后让该值对相关的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 { …Run Code Online (Sandbox Code Playgroud)