Add a DbProviderFactory without an App.Config

前端 未结 8 1750
无人共我
无人共我 2020-12-13 06:52

I am using DbProviderFactories in my data layer (based on Entity Framework) and am using SQLite for my database, but I don\'t have to have a App.Config to have the following

相关标签:
8条回答
  • 2020-12-13 07:34

    LATE ANSWER:

    You can always directly get a factory like this:

    DbProviderFactory factory = System.Data.SQLite.SQLiteFactory.Instance;
    // (note that the rest of the code is still provider-agnostic.)
    

    Or use your IoC container to resolve the DbProviderFactory, e.g.:

    container.RegisterInstance<DbProviderFactory>(SQLiteFactory.Instance);
    

    I prefer not to use the DbProviderFactories.GetFactory because of its limitation of requiring a configuration file (or a hack like in @JoshRiver's answer).

    All DbProviderFactories.GetFactory does is, it looks up the registered assembly-qualified name of the factory type using the provider name, and then it gets the value of the static Instance property using reflection.

    If you don't want to use the configuration, one of the methods above might be more convenient depending on your use case.

    0 讨论(0)
  • 2020-12-13 07:34

    see the following snippet

        public DataProviderManager(string ProviderName)
        {
    
           var  _Provider = DbProviderFactories.GetFactory(ProviderName);
    
        }
    

    you need to pass the ProviderName that in your case is "System.Data.SQLite".

    you don't need to create the app config section. That section is created by SQLite in the machine.config after the SQLite.net provider is installed.

    the whole purpose of the appconfig section is to help you get the list of configured .net providers when you call the following command:

    public GetProvidersList() { DataTable table= DbProviderFactories.GetFactoryClasses(); }

    0 讨论(0)
提交回复
热议问题