# Migration Guide: Core20 ORM to EF Core 8.0
## Overview
This document outlines the process of migrating from the Core20 ORM system to Entity Framework Core 8.0, using the BudgetConfigDefaultSetting implementation as a reference example.

## Database Connection Management Comparison

### Core20 Approach
```csharp
// Core20 uses static SQL context and connection strings
static string SQL_QUERY_FROM = @"[fp].[BudgetConfigDefaultSetting] OBJ";
internal const string UPDATE_TABLE_NAME = "fp.BudgetConfigDefaultSetting";
```
- Uses static SQL contexts
- Manages connections through `SqlContext.Current`
- Relies on connection string keys
- Manual SQL query construction
- Schema is defined in SQL queries

### EF Core Approach
```csharp
public class OnePlanDbContext : DbContext
{
    public DbSet<BudgetConfigDefaultSetting> BudgetConfigDefaultSettings { get; set; }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BudgetConfigDefaultSetting>(entity =>
        {
            entity.ToTable("BudgetConfigDefaultSetting", "fp");
            // ... other configurations
        });
    }
}
```
- Connection management through DbContext
- Dependency injection for context lifecycle
- Fluent API for schema definition
- Type-safe queries
- Connection string in configuration

## ORM Feature Comparison

| Feature | Core20 | EF Core | Migration Effort |
|---------|--------|---------|------------------|
| Schema Definition | SQL Strings | Fluent API/Attributes | Medium |
| Query Building | String Concatenation | LINQ | High |
| Change Tracking | Manual (IsDirty) | Automatic | Low |
| Transactions | Manual | Built-in | Medium |
| Relationships | Manual Loading | Navigation Properties | High |
| Validation | Custom Implementation | Data Annotations/Custom | Medium |
| Bulk Operations | Custom SQL | Extensions Available | Medium |
| Security | Manual SQL Filters | Global Query Filters | Medium |

## Migration Steps

1. **Entity Class Migration**
   ```csharp
   // From Core20:
   [Serializable]
   public partial class BudgetConfigDefaultSetting : ReadWriteBase<BudgetConfigDefaultSetting>
   
   // To EF Core:
   public class BudgetConfigDefaultSetting
   {
       public int SettingId { get; set; }
       public string Name { get; set; }
       public string Description { get; set; }
       public bool DefaultValue { get; set; }
       public DateOnly DateCreated { get; set; }
   }
   ```

2. **Configuration Migration**
   ```csharp
   public class BudgetConfigDefaultSettingConfiguration : IEntityTypeConfiguration<BudgetConfigDefaultSetting>
   {
       public void Configure(EntityTypeBuilder<BudgetConfigDefaultSetting> builder)
       {
           builder.ToTable("BudgetConfigDefaultSetting", "fp");
           builder.HasKey(e => e.SettingId);
           builder.Property(e => e.Name).IsRequired();
           // ... other configurations
       }
   }
   ```

3. **Repository Pattern Implementation**
   - Replace static loading methods with repository methods
   - Convert SQL queries to LINQ expressions
   - Implement unit of work pattern if needed

## Code Examples Comparison

### Loading Records
```csharp
// Core20
var records = BudgetConfigDefaultSetting.LoadByColumn("Name", "SomeName");

// EF Core
var records = await _repository.GetByNameAsync("SomeName");
```

### Saving Changes
```csharp
// Core20
setting.MarkDirty();
setting.Save();

// EF Core
await _repository.UpdateAsync(setting);
```

### Relationships
```csharp
// Core20
var linkedSettings = BudgetConfigDefaultSetting.LoadByLinks("LinkTable", "ColumnName", ids);

// EF Core
var linkedSettings = await _repository.GetByLinksAsync(
    _context.Links.Where(l => ids.Contains(l.Id)),
    link => link.SettingId
);
```

## Migration Effort Assessment

### High Effort Areas
1. **Query Conversion**
   - Converting raw SQL to LINQ expressions
   - Implementing type-safe joins
   - Replacing string-based queries

2. **Relationship Management**
   - Defining navigation properties
   - Converting manual loading to eager/lazy loading
   - Implementing proper relationship configurations

3. **Transaction Management**
   - Replacing manual transaction handling
   - Implementing unit of work pattern
   - Managing context lifecycle

### Medium Effort Areas
1. **Schema Configuration**
   - Converting SQL schema to Fluent API
   - Setting up entity configurations
   - Defining indexes and constraints

2. **Validation Logic**
   - Implementing validation attributes
   - Converting custom validation rules
   - Setting up FluentValidation if needed

### Low Effort Areas
1. **Basic CRUD Operations**
   - Converting simple load/save operations
   - Implementing basic repository methods
   - Setting up entity properties

## Best Practices for Migration

1. **Incremental Migration**
   - Migrate one entity at a time
   - Keep both systems running during migration
   - Implement facade pattern for transition

2. **Testing Strategy**
   - Create integration tests first
   - Verify queries produce same results
   - Test performance impact

3. **Performance Considerations**
   - Use compiled queries for frequent operations
   - Implement proper indexing
   - Monitor query performance

4. **Security Migration**
   - Replace manual SQL filters with Global Query Filters
   - Implement proper user context
   - Audit sensitive operations

## Tools and Utilities

1. **Essential Tools**
   - EF Core Power Tools
   - SQL Server Profiler
   - dotnet ef CLI tools

2. **Recommended Extensions**
   - EF Core Bulk Extensions
   - AutoMapper
   - FluentValidation

## AI Assistance for Migration

### Using AI Tools for Migration Support

The migration process can be significantly accelerated using Claude and WindSurf. Here's how to effectively leverage these tools:

### Using Claude for Architecture and Analysis

Claude excels at high-level analysis and providing detailed implementation guidance. Here are effective prompting strategies:

#### Example 1: Initial Analysis Prompt
```
I need to migrate a Core20 ORM implementation to EF Core 8.0. Here are my source files:

[Paste your Core20 files]

Please:
1. Analyze the current implementation
2. Identify key patterns and functionality
3. Propose an EF Core equivalent structure
4. Provide a sample implementation for one entity
5. List potential challenges and their solutions
```

#### Example 2: Detailed Implementation Prompt
```
Please help me implement the EF Core repository for this entity:

[Paste your entity class]

Requirements:
1. Maintain existing functionality
2. Use async/await pattern
3. Implement proper dependency injection
4. Include repository interface
5. Show example service layer integration
```

### Using WindSurf for Code Generation

WindSurf is particularly effective for handling specific code generation tasks. Here's how to use it effectively:

1. **Repository Migration**
   ```csharp
   // WindSurf prompt:
   // Generate EF Core repository for this Core20 entity:
   // [Insert entity details]
   ```

2. **Entity Configuration**
   ```csharp
   // WindSurf prompt:
   // Create EF Core entity configuration class for:
   // [Insert schema details]
   ```

### Best Practices for AI-Assisted Migration

1. **Preparation**
   - Document all existing functionality
   - Identify critical patterns
   - Create test cases before migration

2. **Prompting Strategy**
   - Use Claude for:
     * Architecture decisions
     * Pattern analysis
     * Complex transformations
     * Documentation generation
   - Use WindSurf for:
     * Code generation
     * Schema translations
     * LINQ query conversions
     * Repetitive transformations

3. **Validation Process**
   - Review AI-generated code thoroughly
   - Test against existing functionality
   - Verify performance characteristics
   - Check security implications

### AI Tools Comparison for Migration Tasks

| Task | Claude | WindSurf | Best Practice |
|------|---------|---------------|---------------|
| Architecture Analysis | Excellent | Limited | Use Claude |
| Code Generation | Good | Excellent | Use WindSurf with Claude's guidance |
| Pattern Recognition | Excellent | Good | Start with Claude, implement with WindSurf |
| Documentation | Excellent | Limited | Use Claude |
| Edge Cases | Good | Good | Use both tools complementarily |

### Tool-Specific Best Practices

#### Claude
- Provide complete context
- Ask for step-by-step explanations
- Request multiple approaches
- Use for architectural decisions

#### WindSurf
- Focus on specific code generation tasks
- Use for repetitive transformations
- Leverage for LINQ query conversions
- Apply to schema migrations

### Recommended Workflow

1. **Analysis Phase (Claude)**
   - System architecture review
   - Pattern identification
   - Migration strategy development

2. **Implementation Phase (WindSurf)**
   - Code generation
   - Entity configurations
   - Repository implementations

3. **Validation Phase (Both)**
   - Code review
   - Test case execution
   - Performance validation

## Conclusion
The migration from Core20 to EF Core represents a significant modernization effort that will result in more maintainable, type-safe, and performant code. By leveraging Claude for strategic planning and WindSurf for implementation, teams can significantly accelerate the migration process while maintaining high quality standards. The key is to use each tool's strengths appropriately and maintain a consistent validation process throughout the migration.