简体   繁体   中英

EF6 Code First Pre-generated views for c#

I want to improve the perfermonce of EF6. So I added the template EF6 Code First Pre-generated views for c#. When running my template I have this error

Error 18 Running transformation: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null. Parameter name: existingConnection at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName) at System.Data.Entity.DbContext..ctor(DbConnection existingConnection, Boolean contextOwnsConnection) at AccountingCore.DomainEntities.DatabaseContext..ctor() in c:\\Users\\wassel\\Desktop\\Tay_Entreprise\\Migrated Taysir\\Taysir Entreprise\\AccountingCore\\DomainEntities\\DatabaseContext.cs:line 26
--- End of inner exception stack trace --- at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at Microsoft.VisualStudio.TextTemplating083B3A1F42B3A6F6984D21F93E2A562F18834A2D2CD4017C917BCF44C5B4413C639EC8000C24911B33CCC0A1B86BE1DE019321BC7BEBB9B61319B229A38834A7.GeneratedTextTransformation.GetEdmx(Assembly efAssembly, Type contextType) at Microsoft.VisualStudio.TextTemplating083B3A1F42B3A6F6984D21F93E2A562F18834A2D2CD4017C917BCF44C5B4413C639EC8000C24911B33CCC0A1B86BE1DE019321BC7BEBB9B61319B229A38834A7.GeneratedTextTransformation.TransformText()

This is my Context Class

public class DatabaseContext : DbContext 
    {
        private static DatabaseContext instance;
        public static DatabaseContext Instance
        {
            get
            {
                return instance;
            }
        }
        private static DbConnection _dbConnection;
        public DatabaseContext() : base(_dbConnection,false)
        {
        }

        public static void InitialiseInstance(DbConnection connection)
        {
            if (instance != null)
            {
                instance.Dispose();
                GC.SuppressFinalize(instance);
            }
            instance = new DatabaseContext(connection);
        }

        public DatabaseContext(DbConnection connection)
            : base(connection,true)
        {
            Database.SetInitializer<DatabaseContext>(new DatabaseInitialiser());

        }

        public DbSet<class1> c1{ get; set; }

The template is trying to instantiate your DbContext to generate the views. It does it using the default ctor. However in your case the default ctor is using the _dbConnection variable which will never be initialized in the app domain the transformation is being run and therefore will have a null value which results in the ArgumentNullException thrown from the base DbContext.

Note that the issue here is actually the pattern you are trying to use. I don't think it is a good idea to have one DbConnection instance or DbContext instance. You should be creating DbContext/DbConnection instances on demand. Note that DbContext is not thread safe. You should also avoid having long lived DbContext instances and lean towards the Unit Of Work pattern where you create a context to do a specific thing and discard the instance when you're done. You can read more on static DbConnection here: SqlConnection Thread-Safe? and probably in many more places.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM