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"
});
}
I think you should point out the relationship between tables
Vehicles class
public class Vehicles
{
public int Id { get; set; }
public VehicleStatus VehicleStatus { get; set; }
public VehicleType VehicleType { get; set; }
}
VehicleStatus class
public class VehicleStatus
{
public int Id { get; set; }
public int VehiclesId { get; set; }
public string Status { get; set; }
}
VehicleType class
public class VehicleType
{
public int Id { get; set; }
public int VehiclesId { get; set; }
public string Category { get; set; }
}
VehicleDBContext
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Vehicles>()
.HasData(new
{
Id = 1,
}, new
{
Id = 2,
}, new
{
Id = 3,
});
builder.Entity<VehicleStatus>()
.HasData(new
{
Id = 1,
Status = "Active",
VehiclesId = 1
}, new
{
Id = 2,
Status = "Sold",
VehiclesId = 2
}, new
{
Id = 3,
Status = "Scrapped",
VehiclesId = 3
});
builder.Entity<VehicleType>()
.HasData(new
{
Id = 1,
VehiclesId = 1,
Category = "Car"
}, new
{
Id = 2,
VehiclesId = 2,
Category = "Van"
}, new
{
Id = 3,
VehiclesId = 3,
Category = "SUV"
});
}
Hope this can help you
Best Regards
yinqiu
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
How to insert the record automatically into the master tables automatically if there is no record while running the application.
Here is a working demo like below:
public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<MvcProj3_1Context>();
SeedData(context);
}
host.Run();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
public static void SeedData(MvcProj3_1Context context)
{
if (!context.VehicleType.Any())
{
var status = new List<VehicleStatus>
{
new VehicleStatus
{
//Id = 1,//if your id is Identity(1,1),no need to specify the id
Status = "Active1"
},
new VehicleStatus
{
Status = "Sold1"
},
new VehicleStatus
{
Status = "Scrapped1"
}
};
context.AddRange(status);
context.SaveChanges();
}
if (!context.VehicleType.Any())
{
var type = new List<VehicleType>
{
new VehicleType
{
Category = "Car11"
},
new VehicleType
{
Category = "Car12"
},
new VehicleType
{
Category = "Car13"
}
};
context.AddRange(type);
context.SaveChanges();
}
}
}
Note:Your DbContext no need using `OnModelCreating` method.
First run the application,I could insert the data to the database if the database has no data:
Second time run the application,the database has record in the two tables so that it would not insert the data again:
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
411 Points
1327 Posts
how can I add important master data if not record there while running the application
Jun 15, 2020 09:04 AM|polachan|LINK
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
Member
70 Points
22 Posts
Re: how can I add important master data if not record there while running the application
Jun 16, 2020 02:29 AM|yinqiu|LINK
Hi polachan
I think you should point out the relationship between tables
Vehicles class
VehicleStatus class
VehicleType class
VehicleDBContext
Hope this can help you
Best Regards
yinqiu
Contributor
2690 Points
874 Posts
Re: how can I add important master data if not record there while running the application
Jun 16, 2020 09:51 AM|Rena Ni|LINK
Hi polachan,
Here is a working demo like below:
Note:Your DbContext no need using `OnModelCreating` method.
First run the application,I could insert the data to the database if the database has no data:
Second time run the application,the database has record in the two tables so that it would not insert the data again:
Best Regards,
Rena