简体   繁体   English

我如何在SharpRepository中使用自定义dbcontext(审核日志)

[英]How can i use custom dbcontext (Audit Log) with sharprepository

I have a custom dbcontext which name is Tracker-enabled DbContext ( https://github.com/bilal-fazlani/tracker-enabled-dbcontext ).I want to use it for audit log 我有一个自定义dbcontext,名称为启用了Tracker的DbContext( https://github.com/bilal-fazlani/tracker-enabled-dbcontext ),我想将其用于审核日志

And how can I implement EFRepository? 以及如何实现EFRepository?

I implemented tracker-enabled-context but i cant solve how override sharp repo commit method. 我实现了tracker-enabled-context,但是我无法解决如何覆盖清晰的repo commit方法。

public class HayEntities : TrackerContext
   {
    static HayEntities()
    {
      Database.SetInitializer<HayEntities>(null);
    }
    public HayEntities() : base(HayEntities)
    {
      this.Configuration.ProxyCreationEnabled = false;
      this.Configuration.LazyLoadingEnabled = true;
      this.Configuration.ValidateOnSaveEnabled = false;
    }
    public DbSet<Dummy> Dummys{ get; set; }  
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Configurations.Add(new DummyConfiguration());

    } }
  }
 public class DummyRepository : ConfigurationBasedRepository<DE.Dummy, long>, IDummyRepository
    {
        private readonly IRepository<DE.Dummy, long> _servisHasarRepository;
        public DummyRepository (HayEntities hayEntities,    ICachingStrategy<DE.Dummy, long> cachingStrategy = null)
        {this.CachingEnabled = false;
            _dummyRepository = new EfRepository<DE.Dummy, long>(hayEntities, cachingStrategy);
        }
public void UpdateOrCreate() {
 //In this area how can override save/commit method
}
  } 

You will want to tell SharpRepository to use an IoC provider to inject the DbContext. 您将要告诉SharpRepository使用IoC提供程序来注入DbContext。 This will take care of getting the proper DbContext for your EfRepository. 这将为您的EfRepository获得适当的DbContext。

If you want to control things based on the configuration and have custom repositories so you can implement your own mehods like UpdateOrCreate() then you would inherit from ConfigurationBasedRepository as you have in the example. 如果要基于配置控制事物并具有自定义存储库,则可以实现自己的方法(如UpdateOrCreate()),则可以像示例中一样从ConfigurationBasedRepository继承。

There are more details on setting up IoC with SharpRepository here: http://fairwaytech.com/2013/02/sharprepository-configuration/ (look in the "Entity Framework and Sharing the DbContext" section) 此处提供有关使用SharpRepository设置IoC的更多详细信息: http ://fairwaytech.com/2013/02/sharprepository-configuration/(在“实体框架和共享DbContext”部分中查找)

First look on NuGet for SharpRepository.Ioc.* to find the specific IoC you are using. 首先,在NuGet for SharpRepository.Ioc。*上查找要使用的特定IoC。 If you are using StructureMap then you would do something like this. 如果您使用的是StructureMap,则可以执行以下操作。

In your StructureMap configuration: 在您的StructureMap配置中:

// Hybrid (once per thread or ASP.NET request if you’re in a web application)
For<DbContext>()
    .HybridHttpOrThreadLocalScoped()
    .Use<HayEntities>()
    .Ctor<string>("connectionString").Is(entityConnectionString);

Then you need to tell SharpRepository to use StructureMap by calling this in your startup code: 然后,您需要通过在启动代码中调用以下代码来告诉SharpRepository使用StructureMap:

RepositoryDependencyResolver.SetDependencyResolver(new StructureMapDependencyResolver(ObjectFactory.Container));

After doing these things, then if you use EfRepository then it will know to ask StructureMap for the DbContext. 完成这些操作后,如果您使用EfRepository,它将知道向StructureMap询问DbContext。

Now in your example above where you are using ConfigurationBasedRepository, I would suggest setting the caching in the configuration file instead of in code since you are using the configuration to load the repository. 现在在上面使用ConfigurationBasedRepository的示例中,我建议在配置文件中而不是在代码中设置缓存,因为您正在使用配置来加载存储库。 Since IoC is handling the DbContext you don't need to do anyhing with that and you can focus on the custom method you want to write. 由于IoC正在处理DbContext,因此您无需执行任何操作,您可以专注于要编写的自定义方法。

public class DummyRepository : ConfigurationBasedRepository<DE.Dummy, long>, IDummyRepository
{
    public void UpdateOrCreate()
    {
        // You have access to the underlying IRepository<> which is going to be an EfRepository in your case assuming you did that in the config file
        // here you can call Repository.Add(), or Reposiory.Find(), etc.
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写自定义dbcontext - how to write custom dbcontext 从基本 DBContext 继承时如何使用 IdentityDBContex - How can I use IdentityDBContex while inheriting from base DBContext 如何设置 DbContext 以将错误记录到 ILogger? - How do I set up a DbContext to log errors to ILogger? EF4 DbContext:如何集中审核数据库读取 - EF4 DbContext: How to centrally audit database reads 我可以指示EF DbContext使用“真实”参数名称吗? - Can I instruct EF DbContext to use “real” parameter names? 我可以在 Entity Framework 4.1 中使用自我跟踪实体和 DBContext 吗? - Can I use Self Tracking Entities and DBContext in Entity Framework 4.1? 如何判断我使用的是ObjectContext API还是DbContext API? - How to tell if I use ObjectContext API or DbContext API? 如何记录 DbContext 进行的 sql 查询? - How to log sql queries made by DbContext? 我可以使DbContext通用吗? - can i make DbContext generic? 我如何通过位于通用存储库中的通用DbContext访问烘焙到自定义DbContext中的特殊功能? - How could I access special functions baked into a Custom DbContext through a Generic DbContext which lives within a Generic Repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM