Exemplo n.º 1
0
 /// <summary>
 /// 初始化 <see cref="ContextServiceBase"/> 类的新实例。
 /// </summary>
 /// <param name="context"></param>
 protected ContextServiceBase(ContextServiceContext context)
 {
     Provider        = context.Options.Provider;
     ContextType     = context.ContextType;
     ServiceProvider = context.ServiceProvider;
     Options         = context.Options;
     InstanceName    = ContextInstanceManager.TryAdd(context.Options);
 }
Exemplo n.º 2
0
 /// <summary>
 /// 初始化 <see cref="ContextServiceBase"/> 类的新实例。
 /// </summary>
 /// <param name="context"></param>
 public ContextServiceBase(EntityContextInitializeContext context)
 {
     InitializeContext = context;
     InstanceName      = ContextInstanceManager.TryAdd(context);
 }
Exemplo n.º 3
0
        /// <summary>
        /// 尝试创建对应的表。
        /// </summary>
        /// <param name="service"></param>
        /// <param name="entityType">实体类型。</param>
        /// <returns></returns>
        public static bool TryCreate(IContextService service, Type entityType)
        {
            var tbGen = service.Provider.GetTableGenerateProvider();

            if (service == null)
            {
                return(false);
            }

            var metadata = EntityMetadataUnity.GetEntityMetadata(entityType);

            var instanceName = ContextInstanceManager.TryAdd(service.InitializeContext);

            var cacheKey = string.Concat(instanceName, ":", entityType.FullName);

            return(cache.GetOrAdd(cacheKey, () =>
            {
                //判断数据表是否已存在
                if (tbGen.IsExists(service.Database, metadata))
                {
                    try
                    {
                        //尝试添加新的字段
                        var properties = tbGen.TryAddFields(service.Database, metadata);
                        if (properties.Any())
                        {
                            //通知 context 仓储已经以身改变
                            service.OnRespositoryChanged?.Invoke(new RespositoryChangedEventArgs
                            {
                                Succeed = true,
                                EntityType = entityType,
                                EventType = RespositoryChangeEventType.AddFields,
                                AddedProperties = new ReadOnlyCollection <IProperty>(properties)
                            });
                        }
                    }
                    catch (Exception exp)
                    {
                        service.OnRespositoryChanged?.Invoke(new RespositoryChangedEventArgs
                        {
                            EntityType = entityType,
                            EventType = RespositoryChangeEventType.AddFields,
                            Exception = exp
                        });
                        return false;
                    }
                }
                else
                {
                    try
                    {
                        //尝试创建数据表
                        if (tbGen.TryCreate(service.Database, metadata))
                        {
                            //通知 context 仓储已经创建
                            service.OnRespositoryChanged?.Invoke(new RespositoryChangedEventArgs
                            {
                                Succeed = true,
                                EntityType = entityType,
                                EventType = RespositoryChangeEventType.CreateTable
                            });
                        }
                    }
                    catch (Exception exp)
                    {
                        service.OnRespositoryChanged?.Invoke(new RespositoryChangedEventArgs
                        {
                            EntityType = entityType,
                            EventType = RespositoryChangeEventType.CreateTable,
                            Exception = exp
                        });
                        return false;
                    }
                }

                return true;
            }));
        }