You are currently viewing Benefits of Using Entity Framework Core in .NET for Object Relational Mapping

Benefits of Using Entity Framework Core in .NET for Object Relational Mapping

public class WeatherDbContext : DbContext

    {

        public DbSet<WeatherLog> WeatherLogs { get; set; }

        public DbSet<UserPreference> UserPreferences { get; set; }

        public DbSet<UserProfile> UserProfiles { get; set; }

        public WeatherDbContext(DbContextOptions<WeatherDbContext> options)

        : base(options) { }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

        {

        }

    }

    public class UserProfile

    {

        [Key]

        public int Id { get; set; }

        public string? FirstName { get; set; }

        public string? LastName { get; set; }

        public string? Age { get; set;}

    }

dotnet ef migrations add AddAgeFieldToUserProfile

public partial class AddAgeToUserProfiles : Migration

        {

            /// <inheritdoc />

            protected override void Up(MigrationBuilder migrationBuilder)

            {

                migrationBuilder.AddColumn<int>(

                    name: "Age",

                    table: "UserProfiles",

                    type: "int",

                    nullable: true);

            }

            /// <inheritdoc />

            protected override void Down(MigrationBuilder migrationBuilder)

            {

                migrationBuilder.DropColumn(

                    name: "Age",

                    table: "UserProfiles");

            }

        }

         dotnet ef database update

    dotnet ef database update 0

    dotnet ef database update {nameOfMigrationToRollbackTo}

    Leave a Reply