/// <summary> /// 尝试创建对应的表。 /// </summary> /// <param name="entityType">实体类型。</param> /// <param name="context"></param> /// <returns></returns> public static bool TryCreate(Type entityType, InternalContext context) { var metadata = EntityMetadataUnity.GetEntityMetadata(entityType); var lazy = new Lazy <bool>(() => { var syntax = context.Database.Provider.GetService <ISyntaxProvider>(); //判断表是否存在 SqlCommand sql = syntax.ExistsTable(metadata.TableName); if (context.Database.ExecuteScalar <int>(sql) == 0) { if (InternalCreate(metadata, syntax, context)) { //通知 context 仓储已经创建 if (context.OnRespositoryCreated != null) { context.OnRespositoryCreated(entityType); } return(true); } return(false); } return(true); }); return(cache.TryGetValue(entityType, () => lazy.Value)); }