you can create an iterface
public interface IBranchable
{
int BranchID {get;set;}
}
then you need to make every entity class that has BranchID to implement IBranchable interface
public partial Member : IBranchable
{
}
and at the end you can can make an extension method to filter
public static ExtensionsMethods
{
public static IEnumerable<IBranchable> GetByBranchId(this IEnumerable<IBranchable> source, int branchId)
{
return source.Where(x=> x.BranchID == branchId);
}
public static IEnumerable<IBranchable> GetByBranchId1(this IEnumerable<IBranchable> source)
{
return source.Where(x=> x.BranchID == 1);
}
}
usually i act like this and the code is a lot more clear and readable
usage :
db.Members.GetByBranchId(1)
db.OtherEntity.GetByBranchId(1)
UPDATE :
if your controller uses directly entity framework, then you could create a wrapper to expose entity sets pre-filtered by your branchId
public class InternalContext: IDisposable
{
protected bool disposed = false;
private YourEntityContext db;
public int BranchId { get; private set;}
public InternalContext(int branchID)
{
db = new YourEntityContext();
BranchId = branchID;
}
public IEnumerable<Member> Members
{
get {
return db.Members.GetByBranchId(BranchID);
}
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
if (disposing)
{
db.Dispose();
}
disposed = true;
}
public void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
usage :
using(var db = new InternalContext(1))
{
var members = db.Members;
}