Exemple #1
0
        public TEntity Clone(TEntity entity)
        {
            var result = new TEntity();

            foreach (var column in MapExtensions.OwnedColumns(this.configuration.GetMap <TEntity>(), true))
            {
                var prop = this.entityType.GetProperty(column.Name);
                if (column.Type.IsValueType())
                {
                    prop.SetValue(result, prop.GetValue(entity));
                }
                else if (column.Type == typeof(string))
                {
                    var val = prop.GetValue(entity) as string;
                    if (val != null)
                    {
                        val = new string(val.ToCharArray());
                    }

                    prop.SetValue(result, val);
                }
                else if (column.Relationship == RelationshipType.ManyToOne || column.Relationship == RelationshipType.OneToOne)
                {
                    // all we want here is to clone the entity and just leave the primary key on
                    var val = prop.GetValue(entity);
                    if (val != null)
                    {
                        var map        = column.Relationship == RelationshipType.ManyToOne ? column.ParentMap : column.OppositeColumn.Map;
                        var primaryKey = map.GetPrimaryKeyValue(val);
                        var field      = this.entityType.GetField(column.DbName);
                        field.SetValue(result, primaryKey);
                    }
                    else
                    {
                        if (!column.IsNullable)
                        {
                            throw new InvalidOperationException(
                                      string.Format(
                                          "The property {0} on {1} is marked as not nullable. You must add some data for it",
                                          column.Name,
                                          result.GetType()));
                        }
                    }
                }
            }

            return(result);
        }