private void AddDomainServiceType(Type domainServiceType) { bool enableClientAccess = TypeDescriptor.GetAttributes(domainServiceType)[typeof(EnableClientAccessAttribute)] != null; // The DomainService Type must be marked with EnableClientAccess attribute if (!enableClientAccess) { this.LogError(string.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_InvalidDomainServiceType, domainServiceType.Name)); return; } // EF CodeFirst tries to initialize the Database when we new up a DbContext. We want to avoid that at design time. // So disable the Database initializer if it is a DbContext based DomainService. Type contextType = DbContextUtilities.GetDbContextType(domainServiceType); if (contextType != null) { // From the context type, get typeof(DbContext) Type dbContextTypeRef = DbContextUtilities.GetDbContextTypeReference(contextType); System.Diagnostics.Debug.Assert(dbContextTypeRef != null, "If we have the DbContext type, typeof(DbContext) should not be null"); DbContextUtilities.SetDbInitializer(contextType, dbContextTypeRef, null); } try { DomainServiceDescription description = this.GetProviderDescription(domainServiceType); if (description != null) { if (description.EntityTypes.Any() || description.DomainOperationEntries.Any(p => p.Operation == DomainOperation.Invoke)) { this._domainServiceDescriptions.Add(description); } else { this.LogWarning(string.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_EmptyDomainService, domainServiceType.Name)); } } } catch (ArgumentException ae) { // Our DPD layer reports problems only by throwing. // Translate these exceptions into clean error logs // so that they appear in the Error window in VS this.LogError(ae.Message); } catch (InvalidOperationException ioe) { this.LogError(ioe.Message); } }
/// <summary> /// Checks if this type is assignable from typeof(DbContext). /// </summary> /// <param name="type">The type to check if it is a DbContext.</param> /// <returns><c>true</c> is the type is a DbContext, <c>false</c> otherwise.</returns> public static bool IsDbContext(this Type type) { // If we have a reference to typeof(DbContext), then check if type is assignable from it. if (DbContextUtilities._dbContextTypeReference != null) { return(DbContextUtilities._dbContextTypeReference.IsAssignableFrom(type)); } else { // If we don't have reference to typeof(DbContext), then compare the base types to see if one of them is the EntityFramework DbContext type. // If we find a match, we also find the DbContext type. So populate the _dbContextTypeReference with that value. Type t = DbContextUtilities.GetDbContextTypeReference(type); if (t != null) { return(true); } else { return(false); } } }