1.First of all create three different environment with i.Debug .To work with local environment ii.Staging Details…
Category: EF 6 code first
EF 6 code first development follows DRY that is why i am writing some about EF code first Development
Entity framework code first Development
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
namespace EFCodeFirst { public class Student { public long StudentId { get; set; } public String Name { get; set; } public ICollection<Course> Courses { get; set; } } public class Course { public long CourseId { get; set; } public String CourseName { get; set; } public long StudentId { get; set; } public Student Student { get; set; } } public class StudentDbContext : DbContext { public StudentDbContext() : base("DB") { } public DbSet<Student> Students { get; set; } public DbSet<Course> Courses { get; set; } } //add MySeed seed=new MySeed(context); in //protected override void Seed(EFSeedConApp.StudentDbContext context) in Configuration class public class MySeed { public MySeed(StudentDbContext context) { Student student = new Student { Name = "akash1" }; List<Course> courses = new List<Course>(); courses.Add(new Course() { CourseName = "Standard 1" }); courses.Add(new Course() { CourseName = "Standard 2" }); courses.Add(new Course() { CourseName = "Standard 3" }); student.Courses = courses; context.Students.Add(student); Student studentTest = new Student { Name = "Test2" }; List<Course> testcourses = new List<Course>(); testcourses.Add(new Course() { Student = studentTest, CourseName = "Test2 Standard 1" }); context.Courses.AddRange(testcourses); } } class Program { /*public abstract class Common { [Key] public long Id { get; set; } public long? AddedBy { get; set; } [Column(TypeName = "datetime2")] public DateTime? AddedDate { get; set; } public long? UpdatedBy { get; set; } [Column(TypeName = "datetime2")] public DateTime? UpdatedDate { get; set; } }*/ static void Main(string[] args) { //command //Install-Package EntityFramework // Enable-Migrations //Add-Migration MyMigration //update-database } } } |
and web.config
1 2 3 4 5 |
<connectionStrings> <add name="DB" connectionString="Data Source=.;Initial Catalog=CodeFirst;Persist Security Info=True;User ID=sa;Password=pass" providerName="System.Data.SqlClient" /> </connectionStrings> |