public void ToTable_sets_table_name_on_entity()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity<Customer>().ToTable("customers");

            Assert.Equal("customers", model.EntityTypes.Single().TableName());
            Assert.True(string.IsNullOrEmpty(model.EntityTypes.Single().Schema()));

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

            Assert.Equal("CUSTOMERS", model.EntityTypes.Single().TableName());
            Assert.True(string.IsNullOrEmpty(model.EntityTypes.Single().Schema()));

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

            Assert.Equal("my.table", model.EntityTypes.Single().TableName());
            Assert.True(string.IsNullOrEmpty(model.EntityTypes.Single().Schema()));

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

            Assert.Equal("my.table", model.EntityTypes.Single().TableName());
            Assert.Equal("my.schema", model.EntityTypes.Single().Schema());
        }
        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));
        }
        public void ToTable_sets_table_name_on_entity()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity <Customer>().ToTable("customers");

            Assert.Equal("customers", model.EntityTypes.Single().TableName());
            Assert.True(string.IsNullOrEmpty(model.EntityTypes.Single().Schema()));

            modelBuilder.Entity(typeof(Customer).FullName).ToTable("CUSTOMERS");

            Assert.Equal("CUSTOMERS", model.EntityTypes.Single().TableName());
            Assert.True(string.IsNullOrEmpty(model.EntityTypes.Single().Schema()));

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

            Assert.Equal("my.table", model.EntityTypes.Single().TableName());
            Assert.True(string.IsNullOrEmpty(model.EntityTypes.Single().Schema()));

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

            Assert.Equal("my.table", model.EntityTypes.Single().TableName());
            Assert.Equal("my.schema", model.EntityTypes.Single().Schema());
        }
        public void Generate_model_snapshot_class()
        {
            var model      = new Metadata.Model();
            var entityType = new EntityType("Entity");

            entityType.SetKey(entityType.AddProperty("Id", typeof(int)));
            model.AddEntityType(entityType);

            var stringBuilder = new IndentedStringBuilder();

            new CSharpModelCodeGenerator().GenerateModelSnapshotClass("MyNamespace", "MyClass", model, stringBuilder);

            Assert.Equal(
                @"using Microsoft.Data.Entity.Metadata;
using System;

namespace MyNamespace
{
    public class MyClass : ModelSnapshot
    {
        public override IModel Model
        {
            get
            {
                var builder = new ModelBuilder();
                builder.Entity(""Entity"")
                    .Properties(ps => ps.Property<int>(""Id""))
                    .Key(""Id"");
                return builder.Model;
            }
        }
    }
}",
                stringBuilder.ToString());
        }
        public void Generate_migration_metadata_class()
        {
            var model      = new Metadata.Model();
            var entityType = new EntityType("Entity");

            entityType.SetKey(entityType.AddProperty("Id", typeof(int)));
            model.AddEntityType(entityType);

            var migration
                = new MigrationMetadata("Name", "Timestamp")
                {
                TargetModel = model
                };

            var codeGenerator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator());
            var stringBuilder = new IndentedStringBuilder();

            codeGenerator.GenerateMigrationMetadataClass("MyNamespace", "MyClass", migration, stringBuilder);

            Assert.Equal(
                @"using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations.Infrastructure;
using System;

namespace MyNamespace
{
    public partial class MyClass : IMigrationMetadata
    {
        string IMigrationMetadata.Name
        {
            get
            {
                return ""Name"";
            }
        }
        
        string IMigrationMetadata.Timestamp
        {
            get
            {
                return ""Timestamp"";
            }
        }
        
        IModel IMigrationMetadata.TargetModel
        {
            get
            {
                var builder = new ModelBuilder();
                builder.Entity(""Entity"")
                    .Properties(ps => ps.Property<int>(""Id""))
                    .Key(""Id"");
                return builder.Model;
            }
        }
    }
}",
                stringBuilder.ToString());
        }
예제 #6
0
        public void ColumnType_sets_annotation_on_entity_property()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity <Customer>().Properties(ps => ps.Property(c => c.Id).ColumnType("bigint"));

            Assert.Equal("bigint", model.EntityTypes.Single().Properties.Single()[MetadataExtensions.Annotations.StorageTypeName]);
        }
예제 #7
0
        public void ColumnName_sets_storage_name_on_entity_property()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity <Customer>().Properties(ps => ps.Property(c => c.Id).ColumnName("id"));

            Assert.Equal("id", model.EntityTypes.Single().Properties.Single().StorageName);
        }
예제 #8
0
        public void ToTable_sets_storage_name_on_entity()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity <Customer>().ToTable("customers");

            Assert.Equal("customers", model.EntityTypes.Single().StorageName);
        }
예제 #9
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());
        }
        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);
        }
예제 #11
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());
        }
예제 #12
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());
        }
예제 #13
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());
        }
예제 #14
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());
        }
예제 #15
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());
        }
예제 #16
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());
        }
예제 #17
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());
        }
예제 #18
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());
        }
        private static IModel CreateModel()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity <FakeEntity>()
            .Key(c => c.Id)
            .Properties(ps => ps.Property(c => c.Value));

            return(model);
        }
예제 #20
0
        private static Metadata.Model CreateModel()
        {
            var model = new Metadata.Model()
            {
                StorageName = "MyDatabase"
            };

            var dependentEntityType = new EntityType("Dependent");

            dependentEntityType.SetTableName("MyTable0");
            dependentEntityType.SetSchema("dbo");

            var principalEntityType = new EntityType("Principal");

            principalEntityType.SetTableName("MyTable1");
            principalEntityType.SetSchema("dbo");

            var dependentProperty = dependentEntityType.AddProperty("Id", typeof(int));
            var principalProperty = principalEntityType.AddProperty("Id", typeof(int));

            var property = dependentEntityType.AddProperty("MyProperty", typeof(string));

            property.SetColumnName("MyColumn");
            property = principalEntityType.AddProperty("MyProperty", typeof(string));
            property.SetColumnName("MyColumn");

            model.AddEntityType(principalEntityType);
            model.AddEntityType(dependentEntityType);

            principalProperty.Annotations.Add(new Annotation(
                                                  MetadataExtensions.Annotations.StorageTypeName, "int"));
            dependentProperty.Annotations.Add(new Annotation(
                                                  MetadataExtensions.Annotations.StorageTypeName, "int"));

            dependentEntityType.SetKey(dependentProperty);
            principalEntityType.SetKey(principalProperty);
            dependentEntityType.GetKey().SetKeyName("MyPK0");
            principalEntityType.GetKey().SetKeyName("MyPK1");

            var foreignKey = dependentEntityType.AddForeignKey(principalEntityType.GetKey(), dependentProperty);

            foreignKey.SetKeyName("MyFK");
            foreignKey.Annotations.Add(new Annotation(
                                           MetadataExtensions.Annotations.CascadeDelete, "True"));

            var index = dependentEntityType.AddIndex(dependentProperty);

            index.SetIndexName("MyIndex");
            index.IsUnique = true;

            return(model);
        }
예제 #21
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 ColumnName_sets_storage_name_on_entity_property()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity<Customer>().Property(c => c.Id).ColumnName("id");

            Assert.Equal("id", model.EntityTypes.Single().Properties.Single().ColumnName());

            modelBuilder.Entity<Customer>().Property<int>("Id").ColumnName("ID");

            Assert.Equal("ID", model.EntityTypes.Single().Properties.Single().ColumnName());
        }
        private static IModel CreateModel()
        {
            var model      = new Metadata.Model();
            var entityType = new EntityType("Entity");
            var property   = entityType.AddProperty("Id", typeof(int));

            entityType.StorageName = "dbo.MyTable";
            entityType.SetKey(property);
            entityType.GetKey().StorageName = "MyPK";
            model.AddEntityType(entityType);

            return(model);
        }
        public void ColumnType_sets_annotation_on_entity_property()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity<Customer>().Property(c => c.Id).ColumnType("bigint");

            Assert.Equal("bigint", model.EntityTypes.Single().Properties.Single()[MetadataExtensions.Annotations.StorageTypeName]);

            modelBuilder.Entity<Customer>().Property<int>("Id").ColumnType("BIGINT");

            Assert.Equal("BIGINT", model.EntityTypes.Single().Properties.Single()[MetadataExtensions.Annotations.StorageTypeName]);
        }
        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);
        }
예제 #26
0
        protected Metadata.Model CreateModel()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            // TODO: Uncomment when complex types are supported
            //builder.ComplexType<Location>();
            modelBuilder.Entity <Customer>(ps =>
            {
                ps.Property(c => c.Name);
                ps.Key(c => c.Id);
                ps.ToTable("Customers");
            });

            return(model);
        }
        private static IModel CreateModel()
        {
            var model = new Metadata.Model {
                StorageName = "MyDatabase"
            };

            var dependentEntityType = new EntityType("Dependent");

            dependentEntityType.SetSchema("dbo");
            dependentEntityType.SetTableName("MyTable0");

            var principalEntityType = new EntityType("Principal");

            principalEntityType.SetSchema("dbo");
            principalEntityType.SetTableName("MyTable1");

            var dependentProperty = dependentEntityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);
            var principalProperty = principalEntityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);

            principalProperty.ValueGenerationOnSave = ValueGenerationOnSave.WhenInserting;

            model.AddEntityType(principalEntityType);
            model.AddEntityType(dependentEntityType);

            principalProperty.Annotations.Add(new Annotation(
                                                  MetadataExtensions.Annotations.StorageTypeName, "int"));
            dependentProperty.Annotations.Add(new Annotation(
                                                  MetadataExtensions.Annotations.StorageTypeName, "int"));

            dependentEntityType.GetOrSetPrimaryKey(dependentProperty);
            principalEntityType.GetOrSetPrimaryKey(principalProperty);
            dependentEntityType.GetPrimaryKey().SetKeyName("MyPK0");
            principalEntityType.GetPrimaryKey().SetKeyName("MyPK1");

            var foreignKey = dependentEntityType.GetOrAddForeignKey(principalEntityType.GetPrimaryKey(), dependentProperty);

            foreignKey.SetKeyName("MyFK");
            foreignKey.Annotations.Add(new Annotation(
                                           MetadataExtensions.Annotations.CascadeDelete, "True"));

            var index = dependentEntityType.GetOrAddIndex(dependentProperty);

            index.SetIndexName("MyIndex");
            index.IsUnique = true;

            return(model);
        }
예제 #28
0
        private static IModel CreateSimpleFKModel()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder
            .Entity <FakeEntity>()
            .Key(c => c.Id)
            .Property(c => c.Value);

            modelBuilder
            .Entity <RelatedFakeEntity>()
            .Key(c => c.Id)
            .ForeignKeys(fk => fk.ForeignKey <FakeEntity>(c => c.Id));

            return(model);
        }
        private static IModel CreateSimpleFKModel()
        {
            var model        = new Metadata.Model();
            var modelBuilder = new BasicModelBuilder(model);

            modelBuilder.Entity <FakeEntity>(b =>
            {
                b.Key(c => c.Id);
                b.Property(c => c.Value);
            });

            modelBuilder.Entity <RelatedFakeEntity>(b =>
            {
                b.Key(c => c.Id);
                b.ForeignKey <FakeEntity>(c => c.Id);
            });

            return(model);
        }
예제 #30
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());
        }
예제 #31
0
        public void Create_migration_metadata()
        {
            var targetModel         = new Metadata.Model();
            var upgradeOperations   = new MigrationOperation[0];
            var downgradeOperations = new MigrationOperation[0];

            var migration
                = new MigrationMetadata("000000000000001_Name")
                {
                TargetModel         = targetModel,
                UpgradeOperations   = upgradeOperations,
                DowngradeOperations = downgradeOperations,
                };

            Assert.Equal("000000000000001_Name", migration.MigrationId);
            Assert.Same(targetModel, migration.TargetModel);
            Assert.Same(upgradeOperations, migration.UpgradeOperations);
            Assert.Same(downgradeOperations, migration.DowngradeOperations);
        }
        public void Create_migration_metadata()
        {
            var targetModel = new Metadata.Model();
            var upgradeOperations = new MigrationOperation[0];
            var downgradeOperations = new MigrationOperation[0];

            var migration
                = new MigrationMetadata("000000000000001_Name")
                    {
                        TargetModel = targetModel,
                        UpgradeOperations = upgradeOperations,
                        DowngradeOperations = downgradeOperations,
                    };

            Assert.Equal("000000000000001_Name", migration.MigrationId);
            Assert.Same(targetModel, migration.TargetModel);
            Assert.Same(upgradeOperations, migration.UpgradeOperations);
            Assert.Same(downgradeOperations, migration.DowngradeOperations);
        }
        private static IModel BuildModel(ValueGenerationOnSave keyStrategy, ValueGenerationOnSave nonKeyStrategy)
        {
            var model = new Metadata.Model();

            var entityType = new EntityType(typeof(T1));

            var key = entityType.AddProperty("Col1", typeof(int));

            key.ValueGenerationOnSave = keyStrategy;
            entityType.SetKey(key);

            var nonKey = entityType.AddProperty("Col2", typeof(string));

            nonKey.ValueGenerationOnSave = nonKeyStrategy;

            model.AddEntityType(entityType);

            return(model);
        }
예제 #34
0
        public void ExportModel(Metadata.Model model)
        {
            Uri uriModel = PackUriHelper.CreatePartUri(new Uri(VpaxFormat.DAXMODEL, UriKind.Relative));

            using (TextWriter tw = new StreamWriter(this.Package.CreatePart(uriModel, "application/json", CompressionOption.Maximum).GetStream(), Encoding.UTF8))
            {
                tw.Write(
                    JsonConvert.SerializeObject(
                        model,
                        Formatting.Indented,
                        new JsonSerializerSettings
                {
                    PreserveReferencesHandling = PreserveReferencesHandling.All,
                    ReferenceLoopHandling      = ReferenceLoopHandling.Serialize
                }
                        )
                    );
                tw.Close();
            }
        }
예제 #35
0
        public void Create_migration_info_with_product_version()
        {
            var targetModel         = new Metadata.Model();
            var upgradeOperations   = new MigrationOperation[0];
            var downgradeOperations = new MigrationOperation[0];

            var migration
                = new MigrationInfo("000000000000001_Name", "1.2.3.4")
                {
                TargetModel         = targetModel,
                UpgradeOperations   = upgradeOperations,
                DowngradeOperations = downgradeOperations,
                };

            Assert.Equal("000000000000001_Name", migration.MigrationId);
            Assert.Equal("1.2.3.4", migration.ProductVersion);
            Assert.Same(targetModel, migration.TargetModel);
            Assert.Same(upgradeOperations, migration.UpgradeOperations);
            Assert.Same(downgradeOperations, migration.DowngradeOperations);
        }
예제 #36
0
        private static IModel CreateModel()
        {
            var model = new Metadata.Model {
                StorageName = "MyDatabase"
            };

            var dependentEntityType = new EntityType("Dependent")
            {
                StorageName = "dbo.MyTable0"
            };
            var principalEntityType = new EntityType("Principal")
            {
                StorageName = "dbo.MyTable1"
            };

            var dependentProperty = dependentEntityType.AddProperty("Id", typeof(int));
            var principalProperty = principalEntityType.AddProperty("Id", typeof(int));

            principalProperty.ValueGenerationStrategy = ValueGenerationStrategy.StoreIdentity;

            model.AddEntityType(principalEntityType);
            model.AddEntityType(dependentEntityType);

            principalProperty.Annotations.Add(new Annotation(
                                                  MetadataExtensions.Annotations.StorageTypeName, "int"));
            dependentProperty.Annotations.Add(new Annotation(
                                                  MetadataExtensions.Annotations.StorageTypeName, "int"));

            dependentEntityType.SetKey(dependentProperty);
            principalEntityType.SetKey(principalProperty);
            dependentEntityType.GetKey().StorageName = "MyPK0";
            principalEntityType.GetKey().StorageName = "MyPK1";

            var foreignKey = dependentEntityType.AddForeignKey(principalEntityType.GetKey(), dependentProperty);

            foreignKey.StorageName = "MyFK";
            foreignKey.Annotations.Add(new Annotation(
                                           MetadataExtensions.Annotations.CascadeDelete, "True"));

            return(model);
        }
예제 #37
0
        private static IModel BuildModel(ValueGenerationStrategy keyStrategy, ValueGenerationStrategy nonKeyStrategy)
        {
            var model = new Metadata.Model();

            var entityType = new EntityType(typeof(T1));

            var key = entityType.AddProperty("Id", typeof(int));

            key.ValueGenerationStrategy = keyStrategy;
            key.StorageName             = "Col1";
            entityType.SetKey(key);

            var nonKey = entityType.AddProperty("Name", typeof(string));

            nonKey.StorageName             = "Col2";
            nonKey.ValueGenerationStrategy = nonKeyStrategy;

            model.AddEntityType(entityType);

            return(model);
        }
예제 #38
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());
        }
예제 #39
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());
        }
        private static IModel CreateCyclicFKModel()
        {
            var model = new Metadata.Model();
            var modelBuilder = new ModelBuilder(model);

            modelBuilder.Entity<FakeEntity>(b =>
                {
                    b.Key(c => c.Id);
                    b.Property(c => c.Value);
                });

            modelBuilder.Entity<RelatedFakeEntity>(b =>
                {
                    b.Key(c => c.Id);
                    b.ForeignKeys(fk => fk.ForeignKey<FakeEntity>(c => c.RelatedId));
                });

            modelBuilder
                .Entity<FakeEntity>()
                .ForeignKeys(fk => fk.ForeignKey<RelatedFakeEntity>(c => c.RelatedId));

            return model;
        }
예제 #41
0
        private static IModel CreateModel()
        {
            var model = new Metadata.Model { StorageName = "MyDatabase" };

            var dependentEntityType = new EntityType("Dependent");
            dependentEntityType.SetSchema("dbo");
            dependentEntityType.SetTableName("MyTable0");

            var principalEntityType = new EntityType("Principal");
            principalEntityType.SetSchema("dbo");
            principalEntityType.SetTableName("MyTable1");

            var dependentProperty = dependentEntityType.AddProperty("Id", typeof(int));
            var principalProperty = principalEntityType.AddProperty("Id", typeof(int));
            principalProperty.ValueGenerationOnSave = ValueGenerationOnSave.WhenInserting;

            model.AddEntityType(principalEntityType);
            model.AddEntityType(dependentEntityType);

            principalProperty.Annotations.Add(new Annotation(
                MetadataExtensions.Annotations.StorageTypeName, "int"));
            dependentProperty.Annotations.Add(new Annotation(
                MetadataExtensions.Annotations.StorageTypeName, "int"));

            dependentEntityType.SetKey(dependentProperty);
            principalEntityType.SetKey(principalProperty);
            dependentEntityType.GetKey().SetKeyName("MyPK0");
            principalEntityType.GetKey().SetKeyName("MyPK1");

            var foreignKey = dependentEntityType.AddForeignKey(principalEntityType.GetKey(), dependentProperty);
            foreignKey.SetKeyName("MyFK");
            foreignKey.Annotations.Add(new Annotation(
                MetadataExtensions.Annotations.CascadeDelete, "True"));

            var index = dependentEntityType.AddIndex(dependentProperty);
            index.SetIndexName("MyIndex");
            index.IsUnique = true;

            return model;
        }
예제 #42
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());
        }
예제 #43
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());
        }
        private static IModel BuildModel(ValueGenerationOnSave keyStrategy, ValueGenerationOnSave nonKeyStrategy)
        {
            var model = new Metadata.Model();

            var entityType = new EntityType(typeof(T1));

            var key = entityType.AddProperty("Id", typeof(int));
            key.ValueGenerationOnSave = keyStrategy;
            key.SetColumnName("Col1");
            entityType.SetKey(key);

            var nonKey = entityType.AddProperty("Name", typeof(string));
            nonKey.SetColumnName("Col2");
            nonKey.ValueGenerationOnSave = nonKeyStrategy;

            model.AddEntityType(entityType);

            return model;
        }
예제 #45
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());
        }
        private static IModel BuildModel(ValueGenerationOnSave keyStrategy, ValueGenerationOnSave nonKeyStrategy)
        {
            var model = new Metadata.Model();

            var entityType = new EntityType(typeof(T1));

            var key = entityType.AddProperty("Id", typeof(int));
            key.ValueGenerationOnSave = keyStrategy;
            key.SetColumnName("Col1");
            entityType.SetKey(key);

            var nonKey = entityType.AddProperty("Name",
                typeof(string),
                shadowProperty: false,
                concurrencyToken: nonKeyStrategy == ValueGenerationOnSave.WhenInsertingAndUpdating);
            nonKey.SetColumnName("Col2");
            nonKey.ValueGenerationOnSave = nonKeyStrategy;

            model.AddEntityType(entityType);

            return model;
        }