Home > front end >  EF Core migration
EF Core migration

Time:10-04

I have a working web application (an end point) containing a few methods and connected to two tables in sql server. This application is fully implemented from scratch by myself in an ashx file and does not follow any new or old architecture, simply some methods in ashx file that are remotely called and handle requirements of client. There are shared DLLs among client and server for data handling. For some reasons I want to upgrade client side to Dot Net core, consequently common DLL needs to be upgraded and finally the end point.

Now I'm facing the problem that EF Core only supports code first, but there are ways for scaffolding . I started with Microsoft tutorials. Then I see There are certain ways for migrating and scaffolding existing database, but I got stuck for hours in first step of using command "dotnet ef dbcontext scaffold "Data Source=..." . Then usually tutorial materials get combined with other technologies like asp.net core very fast, I need to read tons of technologies to do a simple task.

I'm worried I'm going the wrong way. there are only two tables and I can implement table structure by hand. Isn't there any sample code that I can modify it's table definitions and I can restart my project soon? If things are so hard, I will omit EF from my project and redefine the whole end point logic by text sql queries.

CodePudding user response:

I can implement table structure by hand.

Great. Simply create a DbContext subtype that has a DbSet for each of your entities. The only thing scaffolding does is save you time.

Here's a complete example for SQL Server:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Order> Orders { get; } = new HashSet<Order>();
}

public class Order
{

    public int CustomerId { get; set; }
    public int Id { get; set; }
    public Customer Customer { get; set; }
}



public class Db : DbContext
{
    string connectionString = "Server=localhost; database=efcore5test; integrated security = true;TrustServerCertificate=true;";

    public DbSet<Customer> Customers { get; set; }
    public DbSet<Order> Orders{ get; set; }

    public Db(string connectionString) : base()
    {

        this.connectionString = connectionString;
    }
    public Db() : base()
    {
        this.Database.SetCommandTimeout(180);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var constr = this.connectionString;
        
        optionsBuilder.LogTo(Console.WriteLine);

        optionsBuilder.UseSqlServer(constr, o => o.UseRelationalNulls().CommandTimeout(180).UseNetTopologySuite());

        base.OnConfiguring(optionsBuilder);
    }



    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Order>().HasKey(o => new { o.CustomerId, o.Id });
        base.OnModelCreating(modelBuilder);
    }

  
}
  • Related