public void GetMethodChain_returns_chain_when_table()
        {
            var configuration = new JoinTableConfiguration { Table = "Subscriptions" };
            var code = new CSharpCodeHelper();

            Assert.Equal(".Map(m => m.ToTable(\"Subscriptions\"))", configuration.GetMethodChain(code));
        }
        public void GetMethodChain_returns_chain_when_one_right_key()
        {
            var configuration = new JoinTableConfiguration { RightKeys = { "ServiceId" } };
            var code = new CSharpCodeHelper();

            Assert.Equal(".Map(m => m.MapRightKey(\"ServiceId\"))", configuration.GetMethodChain(code));
        }
        public void GetMethodChain_returns_chain_when_one_left_key()
        {
            var configuration = new JoinTableConfiguration { LeftKeys = { "CustomerId" } };
            var code = new CSharpCodeHelper();

            Assert.Equal(".Map(m => m.MapLeftKey(\"CustomerId\"))", configuration.GetMethodChain(code));
        }
Example #4
0
        public void GetMethodChain_returns_chain_when_table()
        {
            var configuration = new JoinTableConfiguration {
                Table = "Subscriptions"
            };
            var code = new CSharpCodeHelper();

            Assert.Equal(".Map(m => m.ToTable(\"Subscriptions\"))", configuration.GetMethodChain(code));
        }
Example #5
0
        public void GetMethodChain_returns_chain_when_one_right_key()
        {
            var configuration = new JoinTableConfiguration {
                RightKeys = { "ServiceId" }
            };
            var code = new CSharpCodeHelper();

            Assert.Equal(".Map(m => m.MapRightKey(\"ServiceId\"))", configuration.GetMethodChain(code));
        }
Example #6
0
        public void GetMethodChain_returns_chain_when_one_left_key()
        {
            var configuration = new JoinTableConfiguration {
                LeftKeys = { "CustomerId" }
            };
            var code = new CSharpCodeHelper();

            Assert.Equal(".Map(m => m.MapLeftKey(\"CustomerId\"))", configuration.GetMethodChain(code));
        }
Example #7
0
        public void GetMethodChain_returns_chain_when_all()
        {
            var configuration = new JoinTableConfiguration
            {
                Table     = "Subscriptions",
                LeftKeys  = { "CustomerId" },
                RightKeys = { "ServiceId" }
            };
            var code = new CSharpCodeHelper();

            Assert.Equal(
                ".Map(m => m.ToTable(\"Subscriptions\").MapLeftKey(\"CustomerId\").MapRightKey(\"ServiceId\"))",
                configuration.GetMethodChain(code));
        }
        public void GetMethodChain_returns_chain_when_all()
        {
            var configuration = new JoinTableConfiguration
                {
                    Table = "Subscriptions",
                    LeftKeys = { "CustomerId" },
                    RightKeys = { "ServiceId" }
                };
            var code = new CSharpCodeHelper();

            Assert.Equal(
                ".Map(m => m.ToTable(\"Subscriptions\").MapLeftKey(\"CustomerId\").MapRightKey(\"ServiceId\"))",
                configuration.GetMethodChain(code));
        }
Example #9
0
        public IFluentConfiguration Discover(NavigationProperty navigationProperty, DbModel model)
        {
            Debug.Assert(navigationProperty != null, "navigationProperty is null.");
            Debug.Assert(model != null, "model is null.");

            var fromEndMember = navigationProperty.FromEndMember;
            var toEndMember   = navigationProperty.ToEndMember;

            if (fromEndMember.RelationshipMultiplicity != RelationshipMultiplicity.Many ||
                toEndMember.RelationshipMultiplicity != RelationshipMultiplicity.Many)
            {
                // Doesn't apply
                return(null);
            }

            var associationSet = model.ConceptualModel.Container.AssociationSets.First(
                s => s.ElementType == navigationProperty.RelationshipType);
            var associationSetMapping = model.ConceptualToStoreMapping.AssociationSetMappings.First(
                m => m.AssociationSet == associationSet);
            var sourceEndColumnProperties = associationSetMapping.SourceEndMapping.PropertyMappings.Select(m => m.Column);
            var targetEndColumnProperties = associationSetMapping.TargetEndMapping.PropertyMappings.Select(m => m.Column);
            var storeEntitySet            = associationSetMapping.StoreEntitySet;
            var fromEndIsSourceEnd        =
                navigationProperty.RelationshipType.RelationshipEndMembers.First() == fromEndMember;
            var leftKeys = fromEndIsSourceEnd
                ? sourceEndColumnProperties.Select(p => p.Name)
                : targetEndColumnProperties.Select(p => p.Name);
            var rightKeys = fromEndIsSourceEnd
                ? targetEndColumnProperties.Select(p => p.Name)
                : sourceEndColumnProperties.Select(p => p.Name);

            // NOTE: Join table names are nondeterministic at runtime, so we'll always
            //       configure them during reverse engineer
            var configuration = new JoinTableConfiguration {
                Table = storeEntitySet.Table ?? storeEntitySet.Name
            };

            if (storeEntitySet.Schema != "dbo")
            {
                configuration.Schema = storeEntitySet.Schema;
            }

            var fromEndEntityType = (EntityType)fromEndMember.GetEntityType();

            if (!fromEndEntityType.KeyMembers.Zip(leftKeys, (m, n) => new { KeyMember = m, KeyName = n })
                .All(p => p.KeyName == p.KeyMember.DeclaringType.Name + "_" + p.KeyMember.Name))
            {
                foreach (var key in leftKeys)
                {
                    configuration.LeftKeys.Add(key);
                }
            }

            var toEndEntityType = toEndMember.GetEntityType();

            if (!toEndEntityType.KeyMembers.Zip(rightKeys, (m, n) => new { KeyMember = m, KeyName = n })
                .All(p => p.KeyName == p.KeyMember.DeclaringType.Name + "_" + p.KeyMember.Name))
            {
                foreach (var key in rightKeys)
                {
                    configuration.RightKeys.Add(key);
                }
            }

            return(configuration);
        }
        public IFluentConfiguration Discover(NavigationProperty navigationProperty, DbModel model)
        {
            Debug.Assert(navigationProperty != null, "navigationProperty is null.");
            Debug.Assert(model != null, "model is null.");

            var fromEndMember = navigationProperty.FromEndMember;
            var toEndMember = navigationProperty.ToEndMember;

            if (fromEndMember.RelationshipMultiplicity != RelationshipMultiplicity.Many
                || toEndMember.RelationshipMultiplicity != RelationshipMultiplicity.Many)
            {
                // Doesn't apply
                return null;
            }

            var associationSet = model.ConceptualModel.Container.AssociationSets.First(
                s => s.ElementType == navigationProperty.RelationshipType);
            var associationSetMapping = model.ConceptualToStoreMapping.AssociationSetMappings.First(
                m => m.AssociationSet == associationSet);
            var sourceEndColumnProperties = associationSetMapping.SourceEndMapping.PropertyMappings.Select(m => m.Column);
            var targetEndColumnProperties = associationSetMapping.TargetEndMapping.PropertyMappings.Select(m => m.Column);
            var storeEntitySet = associationSetMapping.StoreEntitySet;
            var fromEndIsSourceEnd =
                navigationProperty.RelationshipType.RelationshipEndMembers.First() == fromEndMember;
            var leftKeys = fromEndIsSourceEnd
                ? sourceEndColumnProperties.Select(p => p.Name)
                : targetEndColumnProperties.Select(p => p.Name);
            var rightKeys = fromEndIsSourceEnd
                ? targetEndColumnProperties.Select(p => p.Name)
                : sourceEndColumnProperties.Select(p => p.Name);

            // NOTE: Join table names are nondeterministic at runtime, so we'll always
            //       configure them during reverse engineer
            var configuration = new JoinTableConfiguration { Table = storeEntitySet.Table ?? storeEntitySet.Name };

            if (storeEntitySet.Schema != "dbo")
            {
                configuration.Schema = storeEntitySet.Schema;
            }

            var fromEndEntityType = (EntityType)fromEndMember.GetEntityType();

            if (!fromEndEntityType.KeyMembers.Zip(leftKeys, (m, n) => new { KeyMember = m, KeyName = n })
                .All(p => p.KeyName == p.KeyMember.DeclaringType.Name + "_" + p.KeyMember.Name))
            {
                foreach (var key in leftKeys)
                {
                    configuration.LeftKeys.Add(key);
                }
            }

            var toEndEntityType = toEndMember.GetEntityType();

            if (!toEndEntityType.KeyMembers.Zip(rightKeys, (m, n) => new { KeyMember = m, KeyName = n })
                .All(p => p.KeyName == p.KeyMember.DeclaringType.Name + "_" + p.KeyMember.Name))
            {
                foreach (var key in rightKeys)
                {
                    configuration.RightKeys.Add(key);
                }
            }

            return configuration;
        }