public static void Initialize(MultiLingualDbContext context)
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            if (!context.Courses.Any())
            {
                context.Languages.Add(Language.EN);
                context.Languages.Add(Language.ES);
                context.Languages.Add(Language.PT);

                var course1 = new Course()
                {
                    Price = 10
                };
                var course1_t_en = new Course_T()
                {
                    Name = "Course A", Language = Language.EN, Course = course1
                };


                course1.Course_Ts = new List <Course_T>();
                course1.Course_Ts.Add(course1_t_en);

                context.Courses.Add(course1);

                //https://docs.microsoft.com/en-us/ef/core/saving/explicit-values-generated-properties#explicit-values-into-sql-server-identity-columns

                //context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Language ON");
                context.SaveChanges();
                //context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Language OFF");
            }
        }
Example #2
0
        static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService <MultiLingualDbContext>();
                    MultiLingualDbContext.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
            }

            await host.RunAsync();
        }
Example #3
0
 public ConsoleService(ILogger <ConsoleService> logger, MultiLingualDbContext context)
 {
     Logger       = logger;
     this.context = context;
 }