🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# RMOS / code first automatic migrations Enabled database only at runtime kullanımı

1-) C# RMOS - code first automatic migrations Enabled database only at runtime kullanımı

 

açıklama : bu makale runtime migrateyi .Net FrameWork ile anlatır-> .Net Core Değildir.

 

.net frame work için code : migrator.Update();

 

.net core için code : context.Database.Migrate();

 

 

kurulması gereken dll -> Install-Package EntityFramework -Version 6.4.4

 

ilk ders için bu linke bakınız : https://www.roketnot.com/not/1549-code-first-from-database-otomatik-sql-tablolarini-olusturur-entitynin-gelismisi-diyebiliriz

 

.net core için bakınız : https://www.roketnot.com/not/1655-runtime-migration-initial-create-otomatik-sql-database-table-column-olusturma-kullanimi

 

1.ADIM  [App.config]

 

 

...

...

<connectionStrings><add name="ProjeOrnekContext" connectionString="data source=RAMBO3;initial catalog=Databasename;user id=sa;password=123;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /></connectionStrings>

 

</configuration>

 

 

 

2.ADIM

 

 

using System.ComponentModel.DataAnnotations;

using System.ComponentModel.DataAnnotations.Schema;

 

namespace ProjeOrnek.Entities

{

    [Table("Sube")]

    public partial class Sube

    {

        public int id { get; set; }

 

        [StringLength(200)]

        public string ad { get; set; }

 

        public string adres { get; set; }

        [StringLength(50)]

        public string tel { get; set; }

    }

}

 

 

3.ADIM

 

 

using System.Data.Entity;

 

namespace ProjeOrnek.Entities

{

    public partial class ProjeOrnekContext : DbContext

    {

        public ProjeOrnekContext()

            : base("name=ProjeOrnekContext")

        {

        }

 

        public virtual DbSet<Sube> Sube { get; set; }

 

        protected override void OnModelCreating(DbModelBuilder modelBuilder)

        {

        }

    }

}

 

 

4.ADIM

 

Enable-Migrations

 

 

 

5.ADIM Migrations/Configuration.cs

 

 

 public Configuration()

        {

            AutomaticMigrationsEnabled = true;

            AutomaticMigrationDataLossAllowed = true;

        }

 

 

6.ADIM

açıklama : Burası runtime update-database yapar. bu sayede karşı pc de columnlar açılmış olur

 

 

using System;

using System.Data.Entity.Migrations;

using System.Linq;

using System.Windows.Forms;

 

namespace DENEME

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        public void autoUpdateDatabase()

        {

            ProjeOrnekContext context = new ProjeOrnekContext();

            var config = new DbMigrationsConfiguration<ProjeOrnekContext>

            {

                AutomaticMigrationsEnabled = true,

                AutomaticMigrationDataLossAllowed = true,

                ContextKey = nameof(ProjeOrnekContext) // Fixed the issue

            };

            var migrator = new DbMigrator(config);

            //var assemblyMigrations = migrator.GetLocalMigrations();

            //var appliedMigrations = migrator.GetDatabaseMigrations();

            //var pendingMigrations = migrator.GetPendingMigrations();

            migrator.Update();

            var aa = context.Sube.ToList();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            autoUpdateDatabase();

 

            ProjeOrnekContext context = new ProjeOrnekContext();

            var list = context.Sube.ToList();

        }

    }

}

 

 

7. ADIM

 

 

açıklama :  EĞER ALREADY SUBE DERSE YANİ VERİ TABANINDA ZATEN ŞUBE VAR DERSE . VERİ TABANINDAKİ _MigrationHistory i sil

 

 

**** TİPLER ***

 

SQL

C#

 

 

 

 

 

**** KAYNAKLAR ***

 

 

kaynak : https://stackoverflow.com/questions/10334885/entity-framework-code-first-migrate-a-database-known-only-at-runtime

 

kaynak : https://stackoverflow.com/questions/58654006/what-is-the-framework-equivalent-of-getmigrations-getappliedmigrations-and-get

 

Enable-Migrations ÇALIŞMAZSA

 

Install-Package EntityFramework -IncludePrerelease

 

KAYNAK : https://stackoverflow.com/questions/10411529/package-manager-console-enable-migrations-commandnotfoundexception-only-in-a-spe

 

ayrıca : https://stackoverflow.com/questions/44018619/errors-on-add-migration/44019743#44019743

 

ayrıcak : https://stackoverflow.com/questions/58654006/what-is-the-framework-equivalent-of-getmigrations-getappliedmigrations-and-get

 

 

.net core kaynak : https://stackoverflow.com/questions/38238043/how-and-where-to-call-database-ensurecreated-and-database-migrate

 

 

 

 

 

 2023 Şubat 08 Çarşamba
 324