Exemple #1
0
        static void Main(string[] args)
        {
            using (var db = new BloggingContext())
            {
                var typeMapper = db.GetService<IRelationalTypeMapper>();

                Console.WriteLine($"Type mapper in use: {typeMapper.GetType().Name}");
                Console.WriteLine($"Mapping for bool: {typeMapper.GetMapping(typeof(bool)).StoreType}");
                Console.WriteLine($"Mapping for string: {typeMapper.GetMapping(typeof(string)).StoreType}");

                Console.WriteLine("Recreating database from current model");
                Console.WriteLine(" Dropping database...");
                db.Database.EnsureDeleted();
                Console.WriteLine(" Creating database...");
                db.Database.EnsureCreated();

                Console.WriteLine();
                Console.WriteLine("Inserting a new Blog...");
                db.Blogs.Add(new Blog
                {
                    Url = "http://sample.com",
                    Metadata = "<metadata><type>Wordpress</type><owner>rowan</owner></metadata>"
                });

                db.SaveChanges();
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection
                .AddEntityFrameworkSqlServer();

            serviceCollection.AddSingleton<SqlServerTypeMapper, CustomSqlServerTypeMapper>();

            var options = new DbContextOptionsBuilder()
                .UseInternalServiceProvider(serviceCollection.BuildServiceProvider())
                .Options;

            using (var db = new BloggingContext(options))
            {
                var serviceProvider = db.GetInfrastructure<IServiceProvider>();
                var typeMapper = serviceProvider.GetService<IRelationalTypeMapper>();

                Console.WriteLine($"Type mapper in use: {typeMapper.GetType().Name}");
                Console.WriteLine($"Mapping for bool: {typeMapper.GetMapping(typeof(bool)).StoreType}");
                Console.WriteLine($"Mapping for string: {typeMapper.GetMapping(typeof(string)).StoreType}");

                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();

                db.Blogs.Add(new Blog
                {
                    Url = "http://sample.com",
                    Metadata = "<metadata><type>Wordpress</type><owner>rowan</owner></metadata>"
                });

                db.SaveChanges();
            }
        }