Example #1
0
        protected override ISession CreatedSession(string keyspace)
        {
            Cluster cluster;

            if (!_clusters.TryGetValue(_connectionStringBuilder.ClusterName, out cluster))
            {
                return(null);
            }

            var assms       = AppDomain.CurrentDomain.GetAssemblies();
            var session     = cluster.Connect(keyspace ?? string.Empty);
            var context     = _currentDbContext.Context;
            var entityTypes = context.Model.GetEntityTypes();

            foreach (var entityType in entityTypes)
            {
                if (entityType.IsUserDefinedType())
                {
                    var arg           = entityType.ClrType;
                    var udtMap        = typeof(UdtMap);
                    var member        = udtMap.GetMethod("For").MakeGenericMethod(arg);
                    var map           = member.Invoke(udtMap, new object[] { entityType.GetTableName(), _cassandraOptionsExtension.DefaultKeyspace });
                    var genericUdtMap = typeof(UdtMap <>).MakeGenericType(arg);
                    var properties    = typeof(EntityType).GetField("_properties", BindingFlags.Instance | BindingFlags.NonPublic);
                    var props         = properties.GetValue(entityType) as SortedDictionary <string, Property>;
                    foreach (var prop in props)
                    {
                        if (!CassandraMigrationsModelDiffer.CheckProperty(assms, prop.Value))
                        {
                            continue;
                        }

                        var mapMethod = genericUdtMap.GetMethod("Map").MakeGenericMethod(prop.Value.ClrType);
                        var variable  = Expression.Variable(arg, "v");
                        var p         = Expression.Property(variable, prop.Key);
                        var lambda    = Expression.Lambda(p, variable);
                        var colName   = prop.Value.GetColumnName();
                        colName = string.IsNullOrWhiteSpace(colName) ? prop.Value.Name : colName;
                        map     = mapMethod.Invoke(map, new object[] { lambda, colName });
                    }

                    try
                    {
                        session.UserDefinedTypes.Define((UdtMap)map);
                    }
                    catch (InvalidTypeException)
                    {
                        continue;
                    }
                }
            }

            return(session);
        }
Example #2
0
        protected override void GenerateEntityType(
            string builderName,
            IEntityType entityType,
            IndentedStringBuilder stringBuilder)
        {
            var isUserType      = entityType.IsUserDefinedType();
            var ownership       = entityType.FindOwnership();
            var ownerNavigation = ownership?.PrincipalToDependent.Name;

            stringBuilder
            .Append(builderName)
            .Append(
                ownerNavigation != null
                        ? ownership.IsUnique ? ".OwnsOne(" : ".OwnsMany("
                        : ".Entity(")
            .Append(Code.Literal(entityType.Name));

            if (ownerNavigation != null)
            {
                stringBuilder
                .Append(", ")
                .Append(ownerNavigation);
            }

            if (builderName.StartsWith("b", StringComparison.Ordinal))
            {
                var counter = 1;
                if (builderName.Length > 1 &&
                    int.TryParse(builderName.Substring(1), out counter))
                {
                    counter++;
                }

                builderName = "b" + (counter == 0 ? "" : counter.ToString());
            }
            else
            {
                builderName = "b";
            }

            stringBuilder
            .Append(", ")
            .Append(builderName)
            .AppendLine(" =>");
            using (stringBuilder.Indent())
            {
                stringBuilder.Append("{");

                var properties = entityType.GetDeclaredProperties();
                var assms      = AppDomain.CurrentDomain.GetAssemblies();
                using (stringBuilder.Indent())
                {
                    GenerateBaseType(builderName, entityType.BaseType, stringBuilder);
                    GenerateProperties(builderName, entityType.GetDeclaredProperties().Where(p => CassandraMigrationsModelDiffer.CheckProperty(assms, p)), stringBuilder);
                    GenerateKeys(builderName, entityType.GetDeclaredKeys(), entityType.FindDeclaredPrimaryKey(), stringBuilder);
                    GenerateEntityTypeAnnotations(builderName, entityType, stringBuilder);
                    GenerateCheckConstraints(builderName, entityType, stringBuilder);
                    if (ownerNavigation != null)
                    {
                        GenerateRelationships(builderName, entityType, stringBuilder);
                    }

                    GenerateData(builderName, entityType.GetProperties(), entityType.GetSeedData(providerValues: true), stringBuilder);
                }

                stringBuilder
                .AppendLine("});");
            }
        }