.net 如何使实体框架数据上下文只读
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10437058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Entity Framework Data Context Readonly
提问by Harindaka
I need to expose an Entity Framework Data Context to 3rd party plugins. The purpose is to allow these plugins to fetch data only and not to let them issue inserts, updates or deletes or any other database modification commands. Hence how can I make a data context or entity readonly.
我需要向 3rd 方插件公开一个实体框架数据上下文。目的是允许这些插件仅获取数据,而不允许它们发出插入、更新或删除或任何其他数据库修改命令。因此,我怎样才能使数据上下文或实体只读。
回答by bricelam
In addition to connecting with a read-only user, there are a few other things you can do to your DbContext.
除了与只读用户连接之外,您还可以对 DbContext 执行一些其他操作。
public class MyReadOnlyContext : DbContext
{
// Use ReadOnlyConnectionString from App/Web.config
public MyContext()
: base("Name=ReadOnlyConnectionString")
{
}
// Don't expose Add(), Remove(), etc.
public DbQuery<Customer> Customers
{
get
{
// Don't track changes to query results
return Set<Customer>().AsNoTracking();
}
}
public override int SaveChanges()
{
// Throw if they try to call this
throw new InvalidOperationException("This context is read-only.");
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Need this since there is no DbSet<Customer> property
modelBuilder.Entity<Customer>();
}
}
回答by Ehsan Mirsaeedi
As opposed to the accepted answer, I believe it would be better to favor composition over inheritance. Then there would be no need for keeping methods such as SaveChanges to throw an exception. Moreover, why do you need to have such methods in the first place? You should design a class in a way that its consumer doesn't get fooled when it looks at its list of methods. The public interface should be in align with the actual intent and goal of the class while in the accepted answer having SaveChanges doesn't imply that Context is read-only.
与公认的答案相反,我认为更倾向于组合而不是继承。这样就不需要保留诸如 SaveChanges 之类的方法来抛出异常。而且,为什么你首先需要有这样的方法?您应该设计一个类,使其消费者在查看其方法列表时不会被愚弄。公共接口应该与类的实际意图和目标保持一致,而在接受的答案中, SaveChanges 并不意味着 Context 是只读的。
In places where I need to have a read-only context such as in the Read side of CQRSpattern, I use the following implementation. It doesn't provide anything other than Querying capabilities to its consumer.
在需要只读上下文的地方,例如CQRS模式的读取端,我使用以下实现。除了向使用者提供查询功能之外,它不提供任何其他功能。
public class ReadOnlyDataContext
{
private readonly DbContext _dbContext;
public ReadOnlyDataContext(DbContext dbContext)
{
_dbContext = dbContext;
}
public IQueryable<TEntity> Set<TEntity>() where TEntity : class
{
return _dbContext.Set<TEntity>().AsNoTracking();
}
}
By using ReadOnlyDataContext, you can have access to only querying capabilities of DbContext. Let's say you have an entity named Order, then you would use ReadOnlyDataContext instance in a way like below.
通过使用 ReadOnlyDataContext,您只能访问 DbContext 的查询功能。假设您有一个名为 Order 的实体,那么您将按如下方式使用 ReadOnlyDataContext 实例。
readOnlyDataContext.Set<Order>().Where(q=> q.Status==OrderStatus.Delivered).ToArray();

