简体   繁体   English

EF6 Code First预生成的C#视图

[英]EF6 Code First Pre-generated views for c#

I want to improve the perfermonce of EF6. 我想提高EF6的演奏技巧。 So I added the template EF6 Code First Pre-generated views for c#. 因此,我为c#添加了模板EF6 Code First预生成视图。 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. 错误18运行转换:System.Reflection.TargetInvocationException:调用的目标引发了异常。 ---> System.ArgumentNullException: Value cannot be null. ---> System.ArgumentNullException:值不能为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 参数名称:System.Data.Entity.Utilities.Check.NotNull [T](T值,System.Data.Entity.DbContext..ctor(Tb,String parameterName),AccountingCore.DomainEntities.DatabaseContext,DbConnection existingConnection,Boolean contextOwnsConnection) ..ctor()在c:\\ Users \\ wassel \\ Desktop \\ Tay_Entreprise \\ Migrated Taysir \\ Taysir Entreprise \\ AccountingCore \\ DomainEntities \\ DatabaseContext.cs:第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) -内部异常堆栈跟踪的结尾--在System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,布尔skipCheckThis, System.RuntimeType.CreateInstanceDefaultCtor处的布尔值fillCache,StackCrawlMark和stackMark(布尔值publicOnly,布尔值skipCheckThis,布尔值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() 在System.Activator.CreateInstance(类型类型,布尔非公开)在System.Activator.CreateInstance(类型类型)在Microsoft.VisualStudio.TextTemplating083B3A1F42B3A6F6984D21F93E2A562F18834A2D2CD4017C917BCF44C5B4413C639EC8000C24911B33CCC0A1B86BE1DE019321BC7BEBB9B61319B229A38834A7.GeneratedTextTransformation.GetEdmx(大会efAssembly,类型contextType)在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. 该模板正在尝试实例化您的DbContext来生成视图。 It does it using the default ctor. 它使用默认的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. 但是,在您的情况下,默认ctor使用的是_dbConnection变量,该变量永远不会在应用程序域中初始化,正在运行转换,因此将具有null值,这会导致从基本DbContext抛出ArgumentNullException

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. 我认为拥有一个DbConnection实例或DbContext实例不是一个好主意。 You should be creating DbContext/DbConnection instances on demand. 您应该按需创建DbContext / DbConnection实例。 Note that DbContext is not thread safe. 请注意,DbContext不是线程安全的。 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. 您还应该避免DbContext实例寿命长,并倾向于使用Unit of Work模式,在该模式下,您创建上下文来执行特定操作,并在完成后丢弃该实例。 You can read more on static DbConnection here: SqlConnection Thread-Safe? 您可以在此处阅读有关静态DbConnection的更多信息: SqlConnection线程安全吗? and probably in many more places. 可能还有更多地方。

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

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