CloudflareD1.NET.CodeFirst
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package CloudflareD1.NET.CodeFirst --version 1.0.0
NuGet\Install-Package CloudflareD1.NET.CodeFirst -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CloudflareD1.NET.CodeFirst" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CloudflareD1.NET.CodeFirst" Version="1.0.0" />
<PackageReference Include="CloudflareD1.NET.CodeFirst" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CloudflareD1.NET.CodeFirst --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CloudflareD1.NET.CodeFirst, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CloudflareD1.NET.CodeFirst@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CloudflareD1.NET.CodeFirst&version=1.0.0
#tool nuget:?package=CloudflareD1.NET.CodeFirst&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
CloudflareD1.NET.CodeFirst
Code-First ORM for CloudflareD1.NET. Define your database schema using C# classes and attributes, similar to Entity Framework Core.
Features
- Entity Attributes: Define tables, columns, keys, and relationships using attributes
- DbContext Pattern: Familiar API for developers coming from Entity Framework
- Type-Safe Queries: LINQ support through integration with CloudflareD1.NET.Linq
- Migration Generation: Generate migrations from your model classes (coming soon)
- Fluent API: Configure entities using the fluent configuration API
Installation
dotnet add package CloudflareD1.NET.CodeFirst
Quick Start
Define Your Entities
using CloudflareD1.NET.CodeFirst.Attributes;
[Table("users")]
public class User
{
[Key]
[Column("id")]
public int Id { get; set; }
[Required]
[Column("username")]
public string Username { get; set; } = string.Empty;
[Column("email")]
public string? Email { get; set; }
[Column("created_at")]
public DateTime CreatedAt { get; set; }
// Navigation property
public List<Order> Orders { get; set; } = new();
}
[Table("orders")]
public class Order
{
[Key]
[Column("id")]
public int Id { get; set; }
[Required]
[Column("order_number")]
public string OrderNumber { get; set; } = string.Empty;
[Column("user_id")]
[ForeignKey("User")]
public int UserId { get; set; }
// Navigation property
public User User { get; set; } = null!;
}
Create a DbContext
using CloudflareD1.NET;
using CloudflareD1.NET.CodeFirst;
public class MyDbContext : D1Context
{
public MyDbContext(D1Client client) : base(client)
{
}
// Entity sets
public D1Set<User> Users { get; set; } = null!;
public D1Set<Order> Orders { get; set; } = null!;
// Optional: Configure entities with fluent API
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.ToTable("users")
.HasKey(u => u.Id);
modelBuilder.Entity<Order>()
.ToTable("orders")
.HasKey(o => o.Id);
}
}
Use the DbContext
// Initialize D1Client
var options = new D1Options
{
AccountId = "your-account-id",
DatabaseId = "your-database-id",
ApiToken = "your-api-token"
};
var client = new D1Client(options);
// Create context
var context = new MyDbContext(client);
// Query entities using LINQ
var users = await context.Users
.AsQueryable()
.Where("username LIKE ?", "john%")
.ToListAsync();
// Find by primary key
var user = await context.Users.FindAsync(1);
// Get all orders
var allOrders = await context.Orders.ToListAsync();
Attributes
Table Attributes
[Table("name")]: Specifies the database table name for an entity[NotMapped]: Excludes a property from database mapping
Column Attributes
[Column("name", TypeName="TEXT")]: Specifies column name and SQL type[Key]: Marks a property as the primary key[Required]: Makes a column NOT NULL[ForeignKey("PropertyName")]: Defines a foreign key relationship
Entity Conventions
If you don't use attributes, the framework follows these conventions:
- Table Names: Pluralized class name in snake_case (
User→users) - Column Names: Property name in snake_case (
UserId→user_id) - Primary Keys: Properties named
Idor{ClassName}Idbecome primary keys - Types: C# types map to SQLite types automatically:
int,long,short,byte,bool→INTEGERdouble,float,decimal→REALstring,DateTime,Guid→TEXTbyte[]→BLOB
Fluent API
Configure entities programmatically in OnModelCreating:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.ToTable("users")
.Property(u => u.Username)
.IsRequired()
.HasColumnName("user_name");
}
Running Migrations
Apply migrations to your database:
var context = new MyDbContext(client);
var appliedMigrations = await context.MigrateAsync();
foreach (var migration in appliedMigrations)
{
Console.WriteLine($"Applied migration: {migration}");
}
Related Packages
- CloudflareD1.NET: Core client library
- CloudflareD1.NET.Linq: LINQ query support
- CloudflareD1.NET.Migrations: Migration system
Roadmap
- ✅ Entity attributes (Table, Column, Key, ForeignKey, Required, NotMapped)
- ✅ D1Context base class
- ✅ D1Set entity collections
- ✅ ModelBuilder and metadata system
- ✅ Fluent configuration API
- ⏳ Code-first migration generation
- ⏳ Relationship configuration (one-to-many, many-to-one)
- ⏳ Index configuration
- ⏳ Data annotations validation
License
MIT License - see LICENSE file for details
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- CloudflareD1.NET (>= 1.11.2)
- CloudflareD1.NET.Linq (>= 1.10.1)
- CloudflareD1.NET.Migrations (>= 1.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.0.0: Initial release with D1Context, model attributes, relationships, and code-first migration generation.