Пример #1
0
        public void Save(EntityBase parent, LoginInfo user, DataProvider datasource, bool isInTrans)
        {
            //datasource.BeginTran();
            var op = EntityMetaManager.Default[this.GetType()].GetOP(user, datasource);

            op.BeforeSave(this);

            op.blCoustomTran = isInTrans;

            op.Save(this);

            foreach (var p in LoadTypeProperty(GetType(), typeof(IEntity)))
            {
                var o = p.GetValue(this, null);
                if (o != null)
                {
                    ForeignPropertyMeta fp = EntityMetaManager.Default[this.GetType()].GetForeignProperty(p);
                    if (fp != null && !string.IsNullOrEmpty(fp.MasterKey) && !string.IsNullOrEmpty(fp.ForeignKey))
                    {
                        if (this[fp.MasterKey] != null)
                        {
                            ((IEntity)o).SetValue(fp.ForeignKey, this[fp.MasterKey]);
                        }
                    }

                    ((IEntity)o).Save(this, user, datasource, isInTrans);
                }
            }
            ResetStatus(EntityStatus.Original);

            op.AfterSave(this);
        }
Пример #2
0
        TableMeta CollectionEntityMeta(Type entityType)
        {
            var meta = new TableMeta()
            {
                TableName = entityType.Name, EntityType = entityType
            };

            var tableAttrs = entityType.GetCustomAttributes(typeof(EntityTableAttribute), false);

            if (tableAttrs.Length > 0)
            {
                var tableAttr = tableAttrs[0] as EntityTableAttribute;
                meta.TableName = tableAttr.TableName;
                meta.SetKeys(tableAttr.Keys);
                meta.IdentityFieldName = tableAttr.IdentityFieldName;
            }

            foreach (var proInfo in entityType.GetProperties())
            {
                if (proInfo.PropertyType.IsPrimitive ||
                    Type.GetTypeCode(proInfo.PropertyType) == TypeCode.String ||
                    Type.GetTypeCode(proInfo.PropertyType) == TypeCode.DateTime)
                {
                    FieldMeta fm = new FieldMeta(proInfo.Name, Type.GetTypeCode(proInfo.PropertyType));
                    meta.Fields.Add(fm);
                    var propertyAttr = proInfo.GetCustomAttributes(typeof(EntityFieldAttribute), false);
                    if (propertyAttr.Length > 0)
                    {
                        var fAttr = propertyAttr[0] as EntityFieldAttribute;
                        fm.AutoNoSeed = fAttr.AutoNoSeedName;
                        continue;
                    }

                    propertyAttr = proInfo.GetCustomAttributes(typeof(IdentityKeyAttribute), false);
                    if (propertyAttr.Length > 0)
                    {
                        meta.IdentityFieldName = proInfo.Name;
                    }
                }
                else
                {
                    var propertyAttr = proInfo.GetCustomAttributes(typeof(ForeignKeyAttribute), false);
                    if (propertyAttr.Length > 0)
                    {
                        var fkAttr = propertyAttr[0] as ForeignKeyAttribute;
                        var pMeta  = new ForeignPropertyMeta(proInfo.Name)
                        {
                            Property = proInfo, ForeignKey = fkAttr.ForeignKey, MasterKey = fkAttr.MasterKey
                        };
                        meta.ReferProperties.Add(pMeta);
                        if (proInfo.PropertyType.IsSubclassOf(typeof(EntityBase)))
                        {
                            pMeta.DataType = proInfo.PropertyType;
                        }
                        else if (proInfo.PropertyType.IsSubclassOf(typeof(EntityList <>)))
                        {
                            foreach (var t in proInfo.PropertyType.GetGenericArguments())
                            {
                                if (t.IsSubclassOf(typeof(EntityBase)))
                                {
                                    pMeta.DataType     = t;
                                    pMeta.ForgeignType = ForgeignType.Children;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            // throw new ApplicationException("非有效子对象数据类型");
                        }
                    }
                }
            }
            return(meta);
        }