示例#1
0
        public static Entity AddEntity <TEntity>()
        {
            var entity = new Entity(typeof(TEntity));

            EntitiesTypes.Add(entity);

            return(entity);
        }
示例#2
0
        public void AddType <T>() where T : IDbEntity
        {
            Type type = typeof(T);

            if (EntitiesTypes.Contains(type))
            {
                return;
            }

            EntitiesTypes.Add(type);

            IndexTracker.Add(type, 0);

            HashSet <IDbEntity> hashset = new HashSet <IDbEntity>(new DbEntityComparer <IDbEntity>());

            Database.Add(type, hashset);
        }
示例#3
0
 public static Entity GetEntity(string entityName)
 {
     return(EntitiesTypes.FirstOrDefault(x => x.Name == entityName));
 }
示例#4
0
        public static void SetForeignKeysReferences()
        {
            foreach (var entity in EntitiesTypes)
            {
                // Try determine which property is a entity key if is not set
                if (entity.Key.IsNullOrEmpty())
                {
                    var entityKey = entity.Properties.FirstOrDefault(x => x.Name.ToLower() == "id");
                    if (entityKey == null)
                    {
                        entityKey = entity.Properties.FirstOrDefault(x => x.Name.ToLower() == entity.Name.ToLower() + "id");
                        if (entityKey == null)
                        {
                            throw new Exception("Entity does not have a defined key");
                        }
                    }

                    entityKey.IsKey = true;
                    if (entity.LinkKey == null)
                    {
                        entityKey.IsLinkKey = true;
                    }
                }
            }

            foreach (var entity in EntitiesTypes)
            {
                foreach (var property in entity.Properties)
                {
                    if (property.IsForeignKey)
                    {
                        property.ForeignEntity = EntitiesTypes.FirstOrDefault(x => x.Name == property.ForeignEntityName);

                        if (!property.ReferencePropertyName.IsNullOrEmpty())
                        {
                            property.ReferenceProperty = entity.Properties.FirstOrDefault(x => x.Name == property.ReferencePropertyName);
                            if (property.ReferenceProperty != null)
                            {
                                property.ReferenceProperty.IsForeignKey      = true;
                                property.ReferenceProperty.ForeignEntity     = property.ForeignEntity;
                                property.ReferenceProperty.ReferenceProperty = property;
                            }
                            else if (!property.TypeInfo.IsSystemType)
                            {
                                if (property.ForeignEntity != null)
                                {
                                    property.TypeInfo.Type = property.ForeignEntity.Key.FirstOrDefault().TypeInfo.Type;
                                }
                                else
                                {
                                    // by default foreign property is int
                                    property.TypeInfo.Type = typeof(int);
                                }
                            }
                        }
                    }
                }

                foreach (var property in entity.Properties)
                {
                    property.Template = new PropertyTemplate(
                        property.Attributes,
                        property.TypeInfo,
                        property.IsForeignKey);
                }

                entity.SetColumns();
                entity.SetLinkKey();
                entity.PrepareGroups();
            }
        }