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

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

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

            key.ValueGenerationOnSave = keyStrategy;
            key.SetColumnName("Col1");
            entityType.GetOrSetPrimaryKey(key);

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

            nonKey.IsConcurrencyToken = nonKeyStrategy == ValueGenerationOnSave.WhenInsertingAndUpdating;

            nonKey.SetColumnName("Col2");
            nonKey.ValueGenerationOnSave = nonKeyStrategy;

            model.AddEntityType(entityType);

            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.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;
        }
        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;
        }
        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;
        }