public Metadata.Model SetTableNames(Metadata.Model model)
        {
            model.GetEntityType(typeof(Customer)).SetTableName("Customers");
            model.GetEntityType(typeof(Employee)).SetTableName("Employees");
            model.GetEntityType(typeof(Product)).SetTableName("Products");
            model.GetEntityType(typeof(Order)).SetTableName("Orders");
            model.GetEntityType(typeof(OrderDetail)).SetTableName("OrderDetails");

            return(model);
        }
Exemplo n.º 2
0
        public void Can_set_entity_table_name_and_schema()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity <Customer>().ToTable("foo", "schema");

            Assert.Equal("foo", model.GetEntityType(typeof(Customer)).TableName());
            Assert.Equal("schema", model.GetEntityType(typeof(Customer)).Schema());
        }
Exemplo n.º 3
0
        public void Can_set_entity_table_name_and_schema()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
                .Entity<Customer>().ToTable("foo", "schema");

            Assert.Equal("foo", model.GetEntityType(typeof(Customer)).TableName());
            Assert.Equal("schema", model.GetEntityType(typeof(Customer)).Schema());
        }
Exemplo n.º 4
0
        public void Property_column_name_can_be_different_from_name()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity("Customer")
            .Property <string>("Name");

            model.GetEntityType(typeof(Customer)).GetProperty("Name").SetColumnName("CustomerName");

            Assert.Equal("CustomerName", model.GetEntityType(typeof(Customer)).GetProperty("Name").ColumnName());
        }
        public void Generate_multiple_operations_batches_them_properly()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity("Customer",
                                b =>
            {
                b.Property <string>("FirstName");
                b.Property <string>("LastName");
                b.ForRelational().Table("Customers", "dbo");
                b.Key("FirstName", "LastName").ForRelational().Name("CustomerPK");
            });

            modelBuilder.Entity("Order",
                                b =>
            {
                b.Property <int>("Id");
                b.Property <decimal?>("Quantity");
                b.ForRelational().Table("Orders", "dbo");
                b.Key("Id").ForRelational().Name("OrderPK");
            });

            var operations = new List <MigrationOperation>
            {
                new CreateDatabaseOperation("CustomeOrderDb"),
                OperationFactory().CreateTableOperation(model.GetEntityType("Customer")),
                OperationFactory().CreateTableOperation(model.GetEntityType("Order")),
                new DropDatabaseOperation("CustomeOrderDb"),
            };

            var batches = SqlGenerator(model).Generate(operations).ToList();

            Assert.Equal(1, batches.Count);
            Assert.Equal(
                @"CREATE DATABASE ""CustomeOrderDb"";
CREATE TABLE ""dbo"".""Customers"" (
    ""FirstName"" varchar(4000),
    ""LastName"" varchar(4000),
    CONSTRAINT ""CustomerPK"" PRIMARY KEY (""FirstName"", ""LastName"")
);
CREATE TABLE ""dbo"".""Orders"" (
    ""Id"" integer NOT NULL,
    ""Quantity"" decimal(18, 2),
    CONSTRAINT ""OrderPK"" PRIMARY KEY (""Id"")
);
DROP DATABASE ""CustomeOrderDb""", batches[0].Sql);
        }
        public void Generate_when_create_table_with_unique_constraints()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity("E",
                                b =>
            {
                b.Property <int>("Foo").ForRelational().DefaultValue(5);
                var bar = b.Property <int?>("Bar").Metadata;
                var c1  = b.Property <string>("C1").Metadata;
                var c2  = b.Property <string>("C2").Metadata;
                b.ForRelational().Table("MyTable", "dbo");
                b.Key("Foo").ForRelational().Name("MyPK");
                b.Metadata.AddKey(c1).Relational().Name = "MyUC0";
                b.Metadata.AddKey(new[] { bar, c2 }).Relational().Name = "MyUC1";
            });

            var operation = OperationFactory().CreateTableOperation(model.GetEntityType("E"));

            Assert.Equal(
                @"CREATE TABLE ""dbo"".""MyTable"" (
    ""Foo"" integer NOT NULL DEFAULT 5,
    ""Bar"" integer,
    ""C1"" varchar(4000),
    ""C2"" varchar(4000),
    CONSTRAINT ""MyPK"" PRIMARY KEY (""Foo""),
    CONSTRAINT ""MyUC0"" UNIQUE (""C1""),
    CONSTRAINT ""MyUC1"" UNIQUE (""Bar"", ""C2"")
)",
                Generate(operation, model));
        }
Exemplo n.º 7
0
        public void Can_set_entity_table_name_with_dot()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity <Customer>().ToTable("my.table");

            Assert.Equal("my.table", model.GetEntityType(typeof(Customer)).TableName());
        }
Exemplo n.º 8
0
        public void Can_set_entity_table_name_with_dot()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
                .Entity<Customer>().ToTable("my.table");

            Assert.Equal("my.table", model.GetEntityType(typeof(Customer)).TableName());
        }
Exemplo n.º 9
0
        public void Can_set_entity_table_name_when_no_clr_type()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
                .Entity("Customer")
                .ToTable("foo");

            Assert.Equal("foo", model.GetEntityType(typeof(Customer)).TableName());
        }
Exemplo n.º 10
0
        public void Can_set_property_column_name()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity <Customer>()
            .Property(c => c.Name).ColumnName("foo");

            Assert.Equal("foo", model.GetEntityType(typeof(Customer)).GetProperty("Name").ColumnName());
        }
Exemplo n.º 11
0
        public void Can_set_entity_table_name_when_no_clr_type()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity("Customer")
            .ToTable("foo");

            Assert.Equal("foo", model.GetEntityType(typeof(Customer)).TableName());
        }
Exemplo n.º 12
0
        public void Property_column_name_defaults_to_name()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity("Customer")
            .Property <string>("Name");

            Assert.Equal("Name", model.GetEntityType(typeof(Customer)).GetProperty("Name").ColumnName());
        }
Exemplo n.º 13
0
        public void Can_set_property_column_name_when_no_clr_type()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity("Customer")
            .Property <string>("Name").ColumnName("foo");

            Assert.Equal("foo", model.GetEntityType(typeof(Customer)).GetProperty("Name").ColumnName());
        }
Exemplo n.º 14
0
        public void Can_set_foreign_key_name()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity("Customer", b =>
            {
                b.Property <int>("Id");
                b.Key("Id");
            });

            modelBuilder.Entity("Order", b =>
            {
                b.Property <int>("CustomerId");
                b.ForeignKeys(fks => fks.ForeignKey("Customer", "CustomerId").KeyName("FK_Foo"));
            });

            Assert.Equal("FK_Foo", model.GetEntityType(typeof(Order)).ForeignKeys.Single().KeyName());
        }
        public void Generate_when_add_column_operation()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity("E",
                                b =>
            {
                b.Property <int>("Id");
                b.Property <int>("Bar").ForRelational().DefaultValue(5);
                b.Key("Id");
                b.ForRelational().Table("MyTable", "dbo");
            });

            var operation = new AddColumnOperation(
                "dbo.MyTable", OperationFactory().Column(model.GetEntityType("E").GetProperty("Bar")));

            Assert.Equal(
                @"ALTER TABLE ""dbo"".""MyTable"" ADD ""Bar"" integer NOT NULL DEFAULT 5",
                Generate(operation, model));
        }
        public void Generate_when_alter_column_operation_with_nullable()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity("E",
                                b =>
            {
                b.Property <int>("Id");
                b.Property <int?>("Foo");
                b.Key("Id");
                b.ForRelational().Table("MyTable", "dbo");
            });

            var operation = new AlterColumnOperation(
                "dbo.MyTable",
                OperationFactory().Column(model.GetEntityType("E").GetProperty("Foo")),
                isDestructiveChange: false);

            Assert.Equal(
                @"ALTER TABLE ""dbo"".""MyTable"" ALTER COLUMN ""Foo"" integer NULL",
                Generate(operation, model));
        }
        public void Generate_when_create_table_operation()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity("E",
                                b =>
            {
                b.Property <int>("Foo").ForRelational().DefaultValue(5);
                b.Property <int?>("Bar");
                b.ForRelational().Table("MyTable", "dbo");
                b.Key("Foo", "Bar").ForRelational().Name("MyPK");
            });

            var operation = OperationFactory().CreateTableOperation(model.GetEntityType("E"));

            Assert.Equal(
                @"CREATE TABLE ""dbo"".""MyTable"" (
    ""Foo"" integer NOT NULL DEFAULT 5,
    ""Bar"" integer,
    CONSTRAINT ""MyPK"" PRIMARY KEY (""Foo"", ""Bar"")
)",
                Generate(operation, model));
        }
Exemplo n.º 18
0
        public void Can_set_property_column_name()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
                .Entity<Customer>()
                .Property(c => c.Name).ColumnName("foo");

            Assert.Equal("foo", model.GetEntityType(typeof(Customer)).GetProperty("Name").ColumnName());
        }
Exemplo n.º 19
0
        public void Property_column_name_can_be_different_from_name()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
                .Entity("Customer")
                .Property<string>("Name");

            model.GetEntityType(typeof(Customer)).GetProperty("Name").SetColumnName("CustomerName");

            Assert.Equal("CustomerName", model.GetEntityType(typeof(Customer)).GetProperty("Name").ColumnName());
        }
Exemplo n.º 20
0
        public void Property_column_name_defaults_to_name()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
                .Entity("Customer")
                .Property<string>("Name");

            Assert.Equal("Name", model.GetEntityType(typeof(Customer)).GetProperty("Name").ColumnName());
        }
Exemplo n.º 21
0
        public void Can_set_foreign_key_name()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity("Customer", b =>
                {
                    b.Property<int>("Id");
                    b.Key("Id");
                });

            modelBuilder.Entity("Order", b =>
                {
                    b.Property<int>("CustomerId");
                    b.ForeignKeys(fks => fks.ForeignKey("Customer", "CustomerId").KeyName("FK_Foo"));
                });

            Assert.Equal("FK_Foo", model.GetEntityType(typeof(Order)).ForeignKeys.Single().KeyName());
        }
Exemplo n.º 22
0
        public void Can_set_property_column_name_when_no_clr_type()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
                .Entity("Customer")
                .Property<string>("Name").ColumnName("foo");

            Assert.Equal("foo", model.GetEntityType(typeof(Customer)).GetProperty("Name").ColumnName());
        }