Beispiel #1
0
        public IConfiguration Discover(EntitySet entitySet, DbModel model)
        {
            Debug.Assert(entitySet != null, "entitySet is null.");
            Debug.Assert(model != null, "model is null.");

            var storeEntitySet = model.ConceptualToStoreMapping
                                 .EntitySetMappings.First(m => m.EntitySet == entitySet)
                                 .EntityTypeMappings.First()
                                 .Fragments.First()
                                 .StoreEntitySet;
            var tableName = storeEntitySet.GetStoreModelBuilderMetadataProperty("Name")
                            ?? storeEntitySet.Table
                            ?? storeEntitySet.Name;
            var schemaName = storeEntitySet.GetStoreModelBuilderMetadataProperty("Schema") ?? storeEntitySet.Schema;

            if (_pluralizationService.Pluralize(_code.Type(entitySet.ElementType)) == tableName &&
                (string.IsNullOrEmpty(schemaName) || schemaName == "dbo"))
            {
                // By convention
                return(null);
            }

            return(new TableConfiguration {
                Table = tableName, Schema = schemaName != "dbo" ? schemaName : null
            });
        }
        /// <inheritdoc />
        // TODO: Handle formatting elsewhere
        public virtual string GetMethodChain(CodeHelper code)
        {
            Debug.Assert(code != null, "code is null.");
            Debug.Assert(LeftEntityType != null, "LeftEntityType is null.");
            Debug.Assert(LeftNavigationProperty != null, "LeftNavigationProperty is null.");
            Debug.Assert(RightNavigationProperty != null, "RightNavigationProperty is null.");

            var builder = new StringBuilder();
            builder.Append(".Entity");
            builder.Append(code.TypeArgument(code.Type(LeftEntityType)));
            builder.AppendLine("()");

            var rightMultiplicity = RightNavigationProperty.FromEndMember.RelationshipMultiplicity;

            switch (rightMultiplicity)
            {
                case RelationshipMultiplicity.Many:
                    builder.Append("                .HasMany(");
                    builder.Append(code.Lambda(LeftNavigationProperty));
                    builder.Append(")");
                    break;

                case RelationshipMultiplicity.One:
                    builder.Append("                .HasRequired(");
                    builder.Append(code.Lambda(LeftNavigationProperty));
                    builder.Append(")");
                    break;

                case RelationshipMultiplicity.ZeroOrOne:
                    builder.Append("                .HasOptional(");
                    builder.Append(code.Lambda(LeftNavigationProperty));
                    builder.Append(")");
                    break;

                default:
                    Debug.Fail("rightMultiplicity is not a valid RelationshipMultiplicity value.");
                    break;
            }

            builder.AppendLine();

            switch (LeftNavigationProperty.FromEndMember.RelationshipMultiplicity)
            {
                case RelationshipMultiplicity.Many:
                    builder.Append("                .WithMany(");
                    builder.Append(code.Lambda(RightNavigationProperty));
                    builder.Append(")");
                    break;

                case RelationshipMultiplicity.One:
                    Debug.Assert(rightMultiplicity != RelationshipMultiplicity.One, "rightMultiplicity is One.");
                    builder.Append("                .WithRequired(");
                    builder.Append(code.Lambda(RightNavigationProperty));
                    builder.Append(")");
                    break;

                case RelationshipMultiplicity.ZeroOrOne:
                    Debug.Assert(
                        rightMultiplicity != RelationshipMultiplicity.ZeroOrOne,
                        "rightMultiplicity is ZeroOrOne.");
                    builder.Append("                .WithOptional(");
                    builder.Append(code.Lambda(RightNavigationProperty));
                    builder.Append(")");
                    break;

                default:
                    Debug.Fail("LeftNavigationProperty.FromEndMember.RelationshipMultiplicity is not a valid RelationshipMultiplicity value.");
                    break;
            }

            return builder.ToString();
        }
Beispiel #3
0
        /// <inheritdoc />
        // TODO: Handle formatting elsewhere
        public virtual string GetMethodChain(CodeHelper code)
        {
            Debug.Assert(code != null, "code is null.");
            Debug.Assert(LeftEntityType != null, "LeftEntityType is null.");
            Debug.Assert(LeftNavigationProperty != null, "LeftNavigationProperty is null.");
            Debug.Assert(RightNavigationProperty != null, "RightNavigationProperty is null.");

            var builder = new StringBuilder();

            builder.Append(".Entity");
            builder.Append(code.TypeArgument(code.Type(LeftEntityType)));
            builder.AppendLine("()");

            var rightMultiplicity = RightNavigationProperty.FromEndMember.RelationshipMultiplicity;

            switch (rightMultiplicity)
            {
            case RelationshipMultiplicity.Many:
                builder.Append("                .HasMany(");
                builder.Append(code.Lambda(LeftNavigationProperty));
                builder.Append(")");
                break;

            case RelationshipMultiplicity.One:
                builder.Append("                .HasRequired(");
                builder.Append(code.Lambda(LeftNavigationProperty));
                builder.Append(")");
                break;

            case RelationshipMultiplicity.ZeroOrOne:
                builder.Append("                .HasOptional(");
                builder.Append(code.Lambda(LeftNavigationProperty));
                builder.Append(")");
                break;

            default:
                Debug.Fail("rightMultiplicity is not a valid RelationshipMultiplicity value.");
                break;
            }

            builder.AppendLine();

            switch (LeftNavigationProperty.FromEndMember.RelationshipMultiplicity)
            {
            case RelationshipMultiplicity.Many:
                builder.Append("                .WithMany(");
                builder.Append(code.Lambda(RightNavigationProperty));
                builder.Append(")");
                break;

            case RelationshipMultiplicity.One:
                Debug.Assert(rightMultiplicity != RelationshipMultiplicity.One, "rightMultiplicity is One.");
                builder.Append("                .WithRequired(");
                builder.Append(code.Lambda(RightNavigationProperty));
                builder.Append(")");
                break;

            case RelationshipMultiplicity.ZeroOrOne:
                Debug.Assert(
                    rightMultiplicity != RelationshipMultiplicity.ZeroOrOne,
                    "rightMultiplicity is ZeroOrOne.");
                builder.Append("                .WithOptional(");
                builder.Append(code.Lambda(RightNavigationProperty));
                builder.Append(")");
                break;

            default:
                Debug.Fail("LeftNavigationProperty.FromEndMember.RelationshipMultiplicity is not a valid RelationshipMultiplicity value.");
                break;
            }

            return(builder.ToString());
        }