Visual FoxPro to .NET and C#: a step-by-step migration guide
How to convert a legacy Visual FoxPro app to C#, .NET and SQL Server when the old code is the only spec: inventory, data, domain rules, reports, cutover.
By M. Adnan Saleem · · Updated · 10 min read
Over a two-and-a-half-year engagement I migrated 42 financial workflows from a legacy Visual FoxPro system to C# and .NET Core, as the sole engineer. The old system carried two decades of business logic and had no written specification. The domain layer was rebuilt on Entity Framework Core with 42 T-SQL stored procedures against SQL Server, the FoxPro reports were replaced with reporting workflows in a Blazor application, and the old system was retired. This is the order of work I would recommend to anyone facing the same kind of migration.
Why these migrations are hard
Visual FoxPro mixes data, logic and screens in one place. A form can hold validation, calculations and direct table updates, so there is no layer to lift out. Microsoft ended support for it years ago, which means the people who understand the system are often the only documentation left. The risk is not the new code. The risk is losing a rule that nobody remembers is there.
1. Inventory the workflows, not the files
Start from what the business does: each task a user completes from start to finish, such as posting a payment or closing a period. List them, name an owner for each, and note which screens, tables and reports each one touches. That list becomes the plan and the definition of done. In my case there were 42.
2. Treat the old code as the specification
When there is no written spec, the running system is the spec, including its oddities. For each workflow, read the FoxPro code and write down the rules in plain language, then confirm them with someone who uses the system. Record real inputs and the outputs they produce. Those pairs become the tests that the new system must pass.
3. Move the data model deliberately
FoxPro tables are loosely typed, often without enforced keys. Moving to SQL Server is the moment to add what was missing: primary and foreign keys, proper date and decimal types, and constraints for rules that used to live only in forms. Do not copy the old structure column for column. Redesign it, and write a repeatable script that loads the legacy data into the new shape, so the load can be rehearsed many times before the real cutover.
Each FoxPro field type needs a decision, and a few of them hide surprises:
- Character. Fixed width and padded with spaces. Trim on the way in and store as
nvarchar. FoxPro also compares strings by prefix unlessSET EXACTis on, so"OPEN"can match longer values. Find out which comparisons relied on that. - Numeric and Currency. Map to
decimalwith an explicit precision. Currency keeps four decimal places, sodecimal(19,4)preserves it. - Date and DateTime. FoxPro allows an empty date, which is not the same as null. Decide what an empty date means for each column before loading, and make the column nullable where the answer is "unknown".
- Logical. Becomes
bit. Check whether nulls were used as a third state. - Memo. Long text held in a separate
.fptfile beside the table. Copy both files together, and store the text asnvarchar(max). - Deleted rows. Deleting a record only flags it; the row stays in the file until the table is packed. Filter flagged rows out during the load, or they come back from the dead.
- Code pages. Each table records the code page it was written in. Convert to Unicode while loading, and check names and addresses with accented characters afterwards.
One practical note on reading the files: Microsoft's Visual FoxPro OLE DB provider is 32-bit only, so a loader that uses it has to run as a 32-bit process. Exporting from FoxPro to delimited files is the fallback when that is awkward. A minimal loader in C# looks like this:
// Windows only: System.Data.OleDb, process built for x86
using var conn = new OleDbConnection(
@"Provider=VFPOLEDB.1;Data Source=C:\legacy\data\");
conn.Open();
using var cmd = new OleDbCommand(
"SELECT * FROM invoices WHERE NOT DELETED()", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
// Character fields come padded with spaces
var customer = ((string)reader["customer"]).TrimEnd();
// map each field into the new schema here
}NOT DELETED() leaves out rows that were flagged for deletion but never packed. Run the loader into staging tables first, then transform into the new schema in T-SQL, so each rehearsal starts from the same raw copy.
The full type mapping, the staging load with BULK INSERT and SqlBulkCopy, control totals and the rehearsal checklist are in the companion article, Visual FoxPro to SQL Server: a repeatable data migration.
4. Put the rules in a domain layer
Business rules that were scattered across forms belong in one place in the new system: a domain layer in C# that knows nothing about screens or the database. That is what makes the rules testable, and it is what stops the new system from turning into the old one. Entity Framework Core handles ordinary reads and writes. For heavy set-based work, such as period-end calculations over many rows, a stored procedure is often the clearer and faster tool.
Much FoxPro logic is written as a loop over a table that changes one row at a time. In SQL Server the same rule is usually one statement. A loop like this:
SELECT invoices
SCAN FOR status = "OPEN" AND due_date < DATE()
REPLACE status WITH "LATE", fee WITH ROUND(amount * 0.015, 2)
ENDSCANbecomes a single set-based update:
UPDATE invoices
SET status = 'LATE',
fee = ROUND(amount * 0.015, 2)
WHERE status = 'OPEN'
AND due_date < CAST(GETDATE() AS date);From C#, Entity Framework Core 7 and later can express the same rule without loading a single row. ExecuteUpdateAsync sends one UPDATE to SQL Server:
var today = DateTime.Today;
await db.Invoices
.Where(i => i.Status == "OPEN" && i.DueDate < today)
.ExecuteUpdateAsync(set => set
.SetProperty(i => i.Status, "LATE")
.SetProperty(i => i.Fee, i => Math.Round(i.Amount * 0.015m, 2)));The Math.Round call inside the query never runs in .NET. EF Core translates it to T-SQL ROUND, so it rounds a half away from zero, the way FoxPro did. The same call on a value in memory rounds differently, which the rounding trap below explains.
A few FoxPro habits need particular care when you read the old code:
- Macro substitution.
&variablebuilds a command from a string at run time. Search for every place it is used and work out what it can expand to, because that code path is invisible to a plain reading. - Public variables. State shared across forms acts as hidden input to a calculation. List each one a workflow reads and turn it into an explicit parameter.
- Global error handlers. An
ON ERRORroutine can swallow failures and carry on. The old system may have been producing wrong results quietly; decide with the business whether to reproduce or correct each case. - Index expressions. Indexes are often built on expressions such as
UPPER(name), and lookups depend on them. Carry the same behaviour over with a collation or a computed column.
Converting FoxPro code to C#: common equivalents
Most FoxPro functions have a direct C# counterpart. The differences that cause bugs are small: where counting starts, what happens on bad input, and what "empty" means.
| Visual FoxPro | C# | Watch for |
|---|---|---|
SCAN ... ENDSCAN | foreach, or a set-based update | A loop that writes one row at a time is usually one SQL statement. |
LOCATE FOR, SEEK | FirstOrDefault(predicate) | FoxPro moves a record pointer; C# returns the row or null. |
IIF(cond, a, b) | cond ? a : b | None. |
NVL(x, y) | x ?? y | None. |
EMPTY(x) | Depends on the type | True for blank strings, zero, empty dates and .F.. Decide per field, often string.IsNullOrWhiteSpace or == 0. |
ALLTRIM(s) | s.Trim() | Character fields are padded, so most reads need it. |
SUBSTR(s, start, n) | s.Substring(start - 1, n) | FoxPro counts from 1, C# from 0. |
AT(find, s) | s.IndexOf(find) + 1 | FoxPro returns 0 when not found, C# returns -1. |
VAL(s) | decimal.TryParse | VAL("12abc") returns 12; parsing in C# fails instead. |
BETWEEN(x, a, b) | x >= a && x <= b | Both ends are included. |
INLIST(x, a, b, c) | new[] { a, b, c }.Contains(x) | None. |
DATE() | DateTime.Today | Returns midnight as a DateTime; use DateOnly where only the date matters. |
GOMONTH(d, n) | d.AddMonths(n) | Both clamp to the end of a shorter month. |
DIMENSION a[10] | new T[10] or List<T> | FoxPro arrays start at 1. |
The rounding trap
If totals in the new system differ from the old by a cent, check rounding first. FoxPro and T-SQL round a half away from zero. In .NET, Math.Round rounds a half to the nearest even digit unless told otherwise:
Math.Round(2.345m, 2); // 2.34: half goes to the even digit
Math.Round(2.345m, 2, MidpointRounding.AwayFromZero); // 2.35: what FoxPro and T-SQL doPick one rule, state it in the domain layer, and use it everywhere money is rounded. Decide also where rounding happens: per line or on the total. The two give different answers and the old system will have chosen one.
5. Keep data access testable
Hide the database behind a narrow interface, so domain logic can be tested without one and the stored procedures can be tested against a real one. Financial systems reward this more than most: when a number is questioned, you want a test that reproduces it.
6. Leave reports until the data is right
Reports are where users notice differences first, and they depend on everything underneath. Rebuild them after the workflows that feed them, and compare old and new output line by line on the same data. A report that matches is the most convincing evidence that a workflow was migrated correctly.
FoxPro reports live in .frx files that hold the layout, the grouping and the expressions for each field, so a report can contain calculations that appear nowhere else in the code. Open each one and list its expressions before rebuilding it. The inventory, the T-SQL totals, the Blazor screen and the parity tests are in Replacing Visual FoxPro reports with Blazor and SQL Server.
7. Migrate one workflow at a time, then retire
A single big cutover concentrates all the risk in one weekend. Moving one workflow at a time keeps each step small enough to verify and to reverse. Where you can, run old and new side by side for a period and compare results. Retire the old system only when the inventory from step one is fully ticked off.
A cutover checklist
- The data load has run end to end at least three times on a full copy, with row counts and control totals compared each time.
- Every workflow in the inventory has passing tests built from real inputs and outputs.
- Each report has been compared with its old version on the same data.
- Users have worked a full period in the new system while the old one was still available.
- There is a written way back: how to return to the old system, and until what date that is possible.
- The old system is kept read-only for a while after retirement, for questions about history.
What I would tell someone starting
- Budget more time for understanding than for coding. The rewrite is the easy half.
- Keep a written log of every rule you find and every decision you make. It becomes the specification the old system never had.
- Do not improve behaviour while migrating it. Match first, then change, so a difference always means a bug.
Quick answers
The short version
Is Visual FoxPro still supported?
No. Version 9 was the last release, and Microsoft's extended support for it ended in January 2015. Existing applications keep running, but nothing is being fixed.
Can a FoxPro application be converted to C# automatically?
Tools can translate syntax, but the result keeps the old structure: logic inside forms, shared state and row-by-row loops. For a system that will be maintained for years, rewriting workflow by workflow against tests gives a better result than a mechanical translation.
How long does a FoxPro to .NET migration take?
It depends on the number of workflows and on how much of the knowledge is written down. Mine covered 42 workflows as a part-time engagement over two and a half years, with one engineer and no specification.
Should the database move first or the application?
The data model, because everything else is tested against it. Moving the tables to SQL Server early also lets old and new run against comparable data while workflows move across one at a time.
Can .NET read FoxPro tables directly?
Yes, through Microsoft's Visual FoxPro OLE DB provider and the System.Data.OleDb package. The provider is 32-bit and Windows only, so the loader has to run as an x86 process on Windows.
Keep reading
Related articles
- Visual FoxPro forms to Blazor: events, buffering, validationMove a Visual FoxPro form to Blazor and EF Core: read the .scx as a table, move Valid rules into the domain, and replace table buffering with rowversion.
- Replacing Visual FoxPro reports with Blazor and SQL ServerHow to retire FoxPro .frx reports without losing a number: inventory, totals in T-SQL, a Blazor QuickGrid screen, CSV and PDF exports, and parity tests.
- Visual FoxPro to SQL Server: a repeatable data migrationMoving Visual FoxPro tables into SQL Server: a mapping for every field type, deleted rows, code pages, and a staging load with control totals you can rehearse.
The project behind this
Read the case study
What I built, where, and the numbers that came out of it.
Related experience: Visual FoxPro migration, .NET developer.