Create the connection and prepare the database
Fusion wraps an already-open ADO.NET connection. Selecting a provider explicitly configures the SQL rules and type handlers for the chosen database.
Create a Fusion connection
The example opens a SqliteConnection and then calls UseSqlite. For a
different database, use the matching ADO.NET connection and provider extension.
ConnectionFactory.cs
var connection = new SqliteConnection(context.Session.ConnectionString);
try
{
await connection.OpenAsync(context.CancellationToken);
return FusionConnection.Create(connection).UseSqlite();
}
catch
{
await connection.DisposeAsync();
throw;
}
Demo schema and seed data
This script belongs only to the demo. Fusion adapts to existing databases and does not impose this schema or a migration mechanism.
Database/Seed.sql
PRAGMA foreign_keys = ON;
CREATE TABLE Person (
ID INTEGER PRIMARY KEY,
LastName TEXT NOT NULL,
FirstMidName TEXT NOT NULL,
EnrollmentDate TEXT NOT NULL,
Discriminator TEXT NOT NULL
);
CREATE TABLE Department (
DepartmentID INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
Budget REAL NOT NULL,
InstructorID INTEGER NOT NULL,
FOREIGN KEY (InstructorID)
REFERENCES Person(ID)
);
CREATE TABLE Course (
CourseID INTEGER PRIMARY KEY,
Title TEXT NOT NULL,
Credits INTEGER NOT NULL,
DepartmentID INTEGER NOT NULL,
FOREIGN KEY (DepartmentID)
REFERENCES Department(DepartmentID)
);
CREATE TABLE OfficeAssignment (
InstructorID INTEGER PRIMARY KEY,
Location TEXT NOT NULL,
FOREIGN KEY (InstructorID)
REFERENCES Person(ID)
);
CREATE TABLE CourseAssignment (
InstructorID INTEGER NOT NULL,
CourseID INTEGER NOT NULL,
PRIMARY KEY (InstructorID, CourseID),
FOREIGN KEY (InstructorID)
REFERENCES Person(ID),
FOREIGN KEY (CourseID)
REFERENCES Course(CourseID)
);
INSERT INTO Person VALUES
(1, 'Abercrombie', 'Kim', '2005-09-01', 'Instructor'),
(2, 'Fakhouri', 'Fadi', '2002-08-06', 'Instructor'),
(3, 'Harui', 'Roger', '2008-07-01', 'Student'),
(4, 'Kapoor', 'Candace', '2011-09-01', 'Student'),
(5, 'Zheng', 'Roger', '2009-09-01', 'Student');
INSERT INTO Department VALUES
(1, 'English', 350000, 1),
(2, 'Mathematics', 100000, 2),
(3, 'Engineering', 350000, 1),
(4, 'Economics', 100000, 2);
INSERT INTO Course VALUES
(2021, 'Composition', 3, 1),
(2042, 'Literature', 4, 1),
(1045, 'Calculus', 4, 2),
(3141, 'Trigonometry', 4, 2),
(1050, 'Chemistry', 3, 3),
(4022, 'Microeconomics', 3, 4),
(4041, 'Macroeconomics', 3, 4);
INSERT INTO OfficeAssignment VALUES
(1, 'Smith 17'),
(2, 'Gowan 27');
INSERT INTO CourseAssignment VALUES
(1, 1050),
(1, 4022),
(2, 1045),
(2, 4041);