KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I would like to pass a value to the ctor of a DbContext and then have that value enforce "filtering" on the related DbSets. Is this possible...or is there a better approach? Code might look like this: 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; }
Tags (comma-separated)
Save Edits
Cancel