public void CreateModel_uses_DbCompiledModel_from_ModelStore_when_available() { var store = new Mock<DbModelStore>(); var dbCompiledModelInStore = new DbCompiledModel(); store.Setup(c => c.TryLoad(It.IsAny<Type>())).Returns(dbCompiledModelInStore); store.Setup(c => c.Save(It.IsAny<Type>(), It.IsAny<DbModel>())); try { var dependencyResolver = new SingletonDependencyResolver<DbModelStore>(store.Object); MutableResolver.AddResolver<DbModelStore>(dependencyResolver); var mockContext = new Mock<LazyInternalContext>( new Mock<DbContext>().Object, new Mock<IInternalConnection>().Object, null, null, null, null, null) { CallBase = true }; var model = LazyInternalContext.CreateModel(mockContext.Object); Assert.Same(dbCompiledModelInStore, model); store.Verify(c => c.TryLoad(It.IsAny<Type>()), Times.Once(), "should load existing model"); store.Verify(c => c.Save(It.IsAny<Type>(), It.IsAny<DbModel>()), Times.Never(), "should not call Save when loading model from store"); } finally //clean up { MutableResolver.ClearResolvers(); } }
public void CreateModel_does_not_use_ModelStore_for_HistoryContext() { var store = new Mock<DbModelStore>(); var dbCompiledModelInStore = new DbCompiledModel(); store.Setup(c => c.TryLoad(It.IsAny<Type>())).Returns(dbCompiledModelInStore); store.Setup(c => c.Save(It.IsAny<Type>(), It.IsAny<DbModel>())); try { var dependencyResolver = new SingletonDependencyResolver<DbModelStore>(store.Object); MutableResolver.AddResolver<DbModelStore>(dependencyResolver); var mockContext = new Mock<LazyInternalContext>( new MockHistoryContext(), new Mock<IInternalConnection>().Object, null, null, null, null, null) { CallBase = true }; mockContext.Object.ModelProviderInfo = ProviderRegistry.Sql2008_ProviderInfo; var model = LazyInternalContext.CreateModel(mockContext.Object); Assert.NotSame(dbCompiledModelInStore, model); store.Verify(c => c.TryLoad(It.IsAny<Type>()), Times.Never(), "should not call store for HistoryContext"); store.Verify(c => c.Save(It.IsAny<Type>(), It.IsAny<DbModel>()), Times.Never(), "should not call store for HistoryContext"); } finally //clean up { MutableResolver.ClearResolvers(); } }
/// <summary> /// Constructor. /// 构造函数 /// </summary> protected OwDbContext(DbCompiledModel model) : base(model) { Logger = NullLogger.Instance; OwSession = NullOwSession.Instance; EntityChangeEventHelper = NullEntityChangeEventHelper.Instance; }
public void Initialize() { var connection = Effort.DbConnectionFactory.CreateTransient(); this.model = CompiledModels.GetModel<RelationEntity, EmptyEntity>(); this.context = new FeatureDbContext(connection, this.model); }
public static IEdmModel AddDbCompiledModelAnnotation(this IEdmModel model, DbCompiledModel dbCompiledModel) { var annotation = new DbCompiledModelAnnotation(dbCompiledModel); model.SetAnnotationValue(model, annotation); return model; }
public F1Context( DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection, bool lazyLoadingEnabled = true, bool proxyCreationEnabled = true) : base(existingConnection, model, contextOwnsConnection) { SetContextOptions(lazyLoadingEnabled, proxyCreationEnabled); }
private ChebayDBContext(DbCompiledModel model, string name) : base(con, model) { tenant_name = name; Database.SetInitializer<ChebayDBContext>(null); this.Configuration.LazyLoadingEnabled = false; }
/// <summary> /// Constructor. /// </summary> protected QdfDbContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { //Logger = NullLogger.Instance; QdfSession = NullQdfSession.Instance; EntityChangedEventHelper = NullEntityChangedEventHelper.Instance; }
/// <summary> /// Calculates an SHA256 hash of the EDMX from the given code first model. This is the hash stored in /// the database in the EdmMetadata table in EF 4.1/4.2. The hash is always calculated using a v2 schema /// as was generated by EF 4.1/4.2 and with the <see cref = "EdmMetadata" /> entity included in the model. /// </summary> public virtual string Calculate(DbCompiledModel compiledModel) { //Contract.Requires(compiledModel != null); //Contract.Requires(compiledModel.ProviderInfo != null); //Contract.Requires(compiledModel.CachedModelBuilder != null); var providerInfo = compiledModel.ProviderInfo; var modelBuilder = compiledModel.CachedModelBuilder.Clone(); // Add back in the EdmMetadata class because the hash created by EF 4.1 and 4.2 will contain it. EdmMetadataContext.ConfigureEdmMetadata(modelBuilder.ModelConfiguration); var databaseMetadata = modelBuilder.Build(providerInfo).DatabaseMapping.Database; databaseMetadata.Version = 2.0; // Ensures SSDL version matches that created by EF 4.1/4.2 var stringBuilder = new StringBuilder(); using (var xmlWriter = XmlWriter.Create( stringBuilder, new XmlWriterSettings { Indent = true })) { new SsdlSerializer().Serialize( databaseMetadata, providerInfo.ProviderInvariantName, providerInfo.ProviderManifestToken, xmlWriter); } return ComputeSha256Hash(stringBuilder.ToString()); }
/// <summary> /// Constructor. /// </summary> protected AbpDbContext(DbCompiledModel model) : base(model) { Logger = NullLogger.Instance; AbpSession = NullAbpSession.Instance; EntityChangedEventHelper = NullEntityChangedEventHelper.Instance; }
/// <summary> /// Constructor. /// </summary> protected QdfDbContext(DbCompiledModel model) : base(model) { //Logger = NullLogger.Instance; QdfSession = NullQdfSession.Instance; EntityChangedEventHelper = NullEntityChangedEventHelper.Instance; }
public DbContextBase( string nameOrConnectionString, DbCompiledModel model, IExceptionMapper exceptionMapper = null) : base(nameOrConnectionString, model) { SetExceptionMapper(exceptionMapper); }
public DbContextBase( DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection, IExceptionMapper exceptionMapper = null) : base(existingConnection, model, contextOwnsConnection) { SetExceptionMapper(exceptionMapper); }
public EfSteps(DatabaseTestContext testContext) { _testContext = testContext; var dbModelBuilder = new DbModelBuilder(); dbModelBuilder.Entity<SimpleBenchmarkEntity>(); var dbModel = dbModelBuilder.Build(new DbProviderInfo("System.Data.SqlClient", "2012")); _compiledModel = dbModel.Compile(); }
private void CreateModel() { var modelBuilder = new DbModelBuilder(); modelBuilder.Entity<UserImpl>(); modelBuilder.Entity<AssetImpl>(); _logger.Info("Constructed DB model"); var model = modelBuilder.Build(GetConnection()); _logger.Info("Compiled DB model"); _compiledModel = model.Compile(); }
public static DbCompiledModel Model(SqlConnection con) { if ((_builder == null) || (_model == null)) { _builder = new DbModelBuilder(); _builder.Configurations.Add(new ProductConfiguration()); var edm = _builder.Build(con); _model = edm.Compile(); } return _model; }
public DbContextBase( string nameOrConnectionString, DbCompiledModel model, IExceptionMapper exceptionMapper = null, IEntityAdder entityAdder = null, IEntityUpdater entityUpdater = null, IEntityDeleter entityDeleter = null, IEntityDetacher entityDetacher = null) : base(nameOrConnectionString, model) { Init(exceptionMapper, entityAdder, entityUpdater, entityDeleter, entityDetacher); }
/// <summary> /// Constructs a <see cref = "LazyInternalContext" /> for the given <see cref = "DbContext" /> owner that will be initialized /// on first use. /// </summary> /// <param name = "owner">The owner <see cref = "DbContext" />.</param> /// <param name = "internalConnection">Responsible for creating a connection lazily when the context is used for the first time.</param> /// <param name = "model">The model, or null if it will be created by convention</param> public LazyInternalContext( DbContext owner, IInternalConnection internalConnection, DbCompiledModel model, IDbModelCacheKeyFactory cacheKeyFactory = null) : base(owner) { //Contract.Requires(internalConnection != null); _internalConnection = internalConnection; _model = model; _cacheKeyFactory = cacheKeyFactory ?? new DefaultModelCacheKeyFactory(); _createdWithExistingModel = model != null; }
protected void BuildDatabaseModel() { var connection = new SqlConnection(_applicationConfiguration.ConnectionString); var modelBuilder = new DbModelBuilder(); modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); /* modelBuilder.Entity<User>().HasMany(x => x.Groups) .WithMany(x => x.Users) .Map(m => { m.MapLeftKey("UserId"); m.MapRightKey("GroupId"); m.ToTable("UserGroups"); });*/ RegisterPersistanceModels(modelBuilder); var databaseModel = modelBuilder.Build(connection); Model = databaseModel.Compile(); }
protected StudioXZeroTenantDbContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { }
protected StudioXZeroTenantDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { }
/// <inheritdoc /> public ItemTrackingProvider(string connectionString, DbCompiledModel model) : base(connectionString, model) { }
public IdentityDbContext(string connectionString, DbCompiledModel model) : base(connectionString, model) { Configure(); }
public TrackerIdentityContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { }
protected LoggingDbContext(DbCompiledModel model) : base(model) { }
/// <summary> /// 通过使用现有连接来连接到数据库以构造一个新的上下文实例,并从给定模型初始化该实例。如果 contextOwnsConnection 是 false,则释放上下文时将不会释放该连接。 /// </summary> /// <param name="existingConnection">要用于新的上下文的现有连接。</param> /// <param name="model">支持此上下文的模型。</param> /// <param name="contextOwnsConnection">如果设置为 true,则释放上下文时将释放该连接;否则调用方必须释放该连接。</param> protected DbContextBase(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { }
public TrackerIdentityContext(DbCompiledModel model) : base(model) { }
public InheritedDbContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { }
/// <summary> /// 使用约定构造一个新的上下文实例以创建将连接到的数据库的名称,并从给定模型初始化该名称。按照约定,该名称是派生上下文类的全名(命名空间与类名称的组合)。请参见有关这如何用于创建连接的类备注。 /// </summary> /// <param name="model">支持此上下文的模型。</param> protected DbContextBase(DbCompiledModel model) : base(model) { }
public ComercialGeneralesDbContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { }
protected ComercialGeneralesDbContext(DbCompiledModel model) : base(model) { }
protected DevDbContext(DbConnection existingConnection, DbCompiledModel compiledModel, bool contextOwnsConnection) : base(existingConnection, compiledModel, contextOwnsConnection) { }
protected DevDbContext(string connectionStringName, DbCompiledModel compiledModel) : base(connectionStringName, compiledModel) { }
protected DevDbContext(DbCompiledModel compiledModel) : base(compiledModel) { }
/// <summary> /// 可以将给定字符串用作将连接到的数据库的名称或连接字符串来构造一个新的上下文实例,并从给定模型初始化该实例。请参见有关这如何用于创建连接的类备注。 /// </summary> /// <param name="nameOrConnectionString">数据库名称或连接字符串。</param> /// <param name="model">支持此上下文的模型。</param> protected DbContextBase(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { }
public AllTypeKeysContext(DbCompiledModel model) : base(model) { }
protected DataContext(DbCompiledModel model) : base(model) { }
public SpSynthDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { }
public TrackerIdentityContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { }
/// <summary> /// Constructor. /// </summary> protected AbpDbContext(DbCompiledModel model) : base(model) { InitializeDbContext(); }
// <summary> // Initializes the underlying <see cref="ObjectContext" />. // </summary> protected override void InitializeContext() { CheckContextNotDisposed(); if (_objectContext == null) { if (_creatingModel) { throw Error.DbContext_ContextUsedInModelCreating(); } try { var contextInfo = DbContextInfo.CurrentInfo; if (contextInfo != null) { ApplyContextInfo(contextInfo); } _creatingModel = true; if (_createdWithExistingModel) { // A DbCompiledModel was supplied, which means we should just create the ObjectContext from the model. // The connection cannot be an EF connection because it would then contain a second source of model info. if (_internalConnection.ConnectionHasModel) { throw Error.DbContext_ConnectionHasModel(); } Debug.Assert(_model != null); _objectContext = _model.CreateObjectContext <ObjectContext>(_internalConnection.Connection); } else { // No model was supplied, so we should either create one using Code First, or if an EF connection // was supplied then we should use the metadata in that connection to create a model. if (_internalConnection.ConnectionHasModel) { _objectContext = _internalConnection.CreateObjectContextFromConnectionModel(); } else { // The idea here is that for a given derived context type and provider we will only ever create one DbCompiledModel. // The delegate given to GetOrAdd may be executed more than once even though ultimately only one of the // values will make it in the dictionary. The RetryLazy ensures that delegate only gets called // exactly one time, thereby ensuring that OnModelCreating will only ever be called once. BUT, sometimes // the delegate will fail (and throw and exception). This may be due to some resource issue--most notably // a problem with the database connection. In such a situation it makes sense to have the model creation // try again later when the resource issue has potentially been resolved. To enable this RetryLazy will // try again next time GetValue called. We have to pass the context to GetValue so that the next time it tries // again it will use the new connection. var key = _cacheKeyFactory(Owner); var model = _cachedModels.GetOrAdd( key, t => new RetryLazy <LazyInternalContext, DbCompiledModel>(CreateModel)).GetValue(this); _objectContext = model.CreateObjectContext <ObjectContext>(_internalConnection.Connection); // Don't actually set the _model unless we succeed in creating the object context. _model = model; } } _objectContext.ContextOptions.EnsureTransactionsForFunctionsAndCommands = _initialEnsureTransactionsForFunctionsAndCommands; _objectContext.ContextOptions.LazyLoadingEnabled = _initialLazyLoadingFlag; _objectContext.ContextOptions.ProxyCreationEnabled = _initialProxyCreationFlag; _objectContext.ContextOptions.UseCSharpNullComparisonBehavior = !_useDatabaseNullSemanticsFlag; _objectContext.ContextOptions.DisableFilterOverProjectionSimplificationForCustomFunctions = _disableFilterOverProjectionSimplificationForCustomFunctions; _objectContext.CommandTimeout = _commandTimeout; _objectContext.ContextOptions.UseConsistentNullReferenceBehavior = true; _objectContext.InterceptionContext = _objectContext.InterceptionContext.WithDbContext(Owner); ResetDbSets(); _objectContext.InitializeMappingViewCacheFactory(Owner); } finally { _creatingModel = false; } } }
public InheritedDbContext(DbCompiledModel model) : base(model) { }
/// <inheritdoc /> public ItemTrackingProvider(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { }
protected StudioXZeroTenantDbContext(DbCompiledModel model) : base(model) { }
private static DbCompiledModel GetOrBuildDbCompoledModel(DbConnection connection) { if ( _s_compiledModel == null ) { lock ( _s_compiledModelSyncRoot ) { if ( _s_compiledModel == null ) { var modelBuilder = new DbModelBuilder(); modelBuilder.Conventions.Add(new NoUnderscoreForeignKeyNamingConvention()); var objectSetProducts = modelBuilder.Entity<EntityObject_Product>().HasEntitySetName("Product").ToTable("Products"); var objectSetOrders = modelBuilder.Entity<EntityObject_Order>().HasEntitySetName("Order").ToTable("Orders"); var objectSetOrderLines = modelBuilder.Entity<EntityObject_OrderLine>().HasEntitySetName("OrderLine").ToTable("OrderLines"); var model = modelBuilder.Build(connection); _s_compiledModel = model.Compile(); } } } return _s_compiledModel; }
public InheritedDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { }
public ComercialGeneralesDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { }
/// <summary> /// Constructor. /// </summary> protected AbpDbContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { AbpSession = NullAbpSession.Instance; }
private static DbCompiledModel GetOrBuildDbCompiledModel(DbConnection connection) { if ( _s_compiledModel == null ) { lock ( _s_compiledModelSyncRoot ) { if ( _s_compiledModel == null ) { var modelBuilder = new DbModelBuilder(); modelBuilder.Conventions.Add(new NoUnderscoreForeignKeyNamingConvention()); var objectSetProducts = modelBuilder.Entity<EntityObject_Product>().HasEntitySetName("Product").ToTable("MY_PRODUCTS"); var objectSetOrders = modelBuilder.Entity<EntityObject_Order>().HasEntitySetName("Order").ToTable("MY_ORDERS"); var objectSetOrderLines = modelBuilder.Entity<EntityObject_OrderLine>().HasEntitySetName("OrderLine").ToTable("MY_ORDER_LINES"); objectSetProducts.Property(p => p.Price).HasColumnName("MY_SPECIAL_PRICE_COLUMN").HasColumnType("MONEY"); objectSetOrderLines.HasRequired(p => p.Product).WithRequiredDependent().Map(m => m.MapKey("MY_SPECIAL_PRODUCT_ID_COLUMN")); objectSetOrderLines.HasRequired(p => p.Order).WithMany(o => o.OrderLines).Map(m => m.MapKey("OrderId")); objectSetOrders.Property(p => p.Id).HasColumnName("MY_SPECIAL_ORDER_ID_COLUMN"); var model = modelBuilder.Build(connection); _s_compiledModel = model.Compile(); } } } return _s_compiledModel; }
/// <summary> /// Initializes the internal context, discovers and initializes sets, and initializes from a model if one is provided. /// </summary> private void InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model = null) { DbConfigurationManager.Instance.EnsureLoadedForContext(GetType()); _internalContext = new LazyInternalContext( this, internalConnection, model , DbConfiguration.DependencyResolver.GetService <Func <DbContext, IDbModelCacheKey> >() , DbConfiguration.DependencyResolver.GetService <AttributeProvider>()); DiscoverAndInitializeSets(); }
/// <summary> /// Constructor. /// </summary> protected AbpDbContext(DbCompiledModel model) : base(model) { AbpSession = NullAbpSession.Instance; }
protected DbContext(DbCompiledModel model) { Check.NotNull(model, "model"); InitializeLazyInternalContext(new LazyInternalConnection(GetType().DatabaseName()), model); }
/// <summary> /// Constructor. /// </summary> protected AbpDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { AbpSession = NullAbpSession.Instance; }
/// <inheritdoc /> /// <summary> /// Constructor. /// </summary> protected StoveDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { InitializeDbContext(); }
public SpSynthDbContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { }
/// <inheritdoc /> /// <summary> /// Constructor. /// </summary> protected StoveDbContext(DbCompiledModel model) : base(model) { InitializeDbContext(); }
/// <summary> /// Constructor. /// </summary> protected AbpDbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection) { InitializeDbContext(); }
/// <inheritdoc /> /// <summary> /// Constructor. /// </summary> protected StoveDbContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { InitializeDbContext(); }
/// <summary> /// Constructor. /// </summary> protected AbpDbContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model) { InitializeDbContext(); }
/// <summary> /// Constructs a new context instance using conventions to create the name of /// the database to which a connection will be made, and initializes it from /// the given model. The by-convention name is the full name (namespace + class /// name) of the derived context class. See the class remarks for how this is /// used to create a connection. /// </summary> /// <param name="model">The model that will back this context.</param> public IdentityDbContext(DbCompiledModel model) : base(model) { }