Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

how can I add important master data if not record there while running the application

$
0
0

Hi

How  to insert the  record automatically into the master tables automatically  if there is no record while running the application.  At the moment I am doing given way but it only works while running database migration time.  I want to fill the record into master table for necessary drop down list .the record should be inserted only if there is no any record exist on the table VehicleStatus,VehicleType. Please  help

public VehicleDBContext(DbContextOptions<VehicleDBContext> options) : base(options)
        {

        }
        public DbSet<Vehicles> Vehicles { get; set; }
        public DbSet<VehicleStatus> VehicleStatus { get; set; }
        public DbSet<VehicleType> VehicleType { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<VehicleStatus>()
                .HasData(new
                {
                    Id = 1,
                    Status = "Active"
                }, new
                {
                    Id = 2,
                    Status = "Sold"
                }, new
                {
                    Id = 3,
                    Status = "Scrapped"
                });


            builder.Entity<VehicleType>()
                .HasData(new
                {
                    Id = 1,
                    Category = "Car"

                }, new
                {
                    Id = 2,
                    Category = "Van"
                }, new
                {
                    Id = 3,
                    Category = "SUV"
                });
        }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>