Valobtify.EFCore.SqlServer
1.0.0
dotnet add package Valobtify.EFCore.SqlServer --version 1.0.0
NuGet\Install-Package Valobtify.EFCore.SqlServer -Version 1.0.0
<PackageReference Include="Valobtify.EFCore.SqlServer" Version="1.0.0" />
paket add Valobtify.EFCore.SqlServer --version 1.0.0
#r "nuget: Valobtify.EFCore.SqlServer, 1.0.0"
// Install Valobtify.EFCore.SqlServer as a Cake Addin #addin nuget:?package=Valobtify.EFCore.SqlServer&version=1.0.0 // Install Valobtify.EFCore.SqlServer as a Cake Tool #tool nuget:?package=Valobtify.EFCore.SqlServer&version=1.0.0
Table of Contents
Overview
Valobtify.EFCore.SqlServer
is an extension of the Valobtify
library that simplifies the configuration and persistence of single-value objects in Entity Framework Core. It automates the application of data annotations such as MaxLength
and handles type conversions, making your value objects database-ready with minimal setup.
Installation
To install the Valobtify.EFCore.SqlServer
package, use the following command in your terminal:
dotnet add package Valobtify.EFCore.SqlServer
Ensure that you have the required .NET SDK installed.
Setting Up
To configure single-value objects in your Entity Framework Core DbContext
, override the OnModelCreating
method as shown below:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.SetupSingleValueObjects();
base.OnModelCreating(modelBuilder);
}
Explanation
The SetupSingleValueObjects
method automatically applies the necessary configurations for your single-value objects, including data annotations and type conversions. By calling this method, you ensure your value objects are properly mapped to the database schema.
Configuring Max Length
You can configure the maximum length for single-value objects in two ways:
1. Using MaxLengthAttribute
public sealed class Name : SingleValueObject<Name, string>, ICreatableValueObject<Name, string>
{
[MaxLength(20)]
public override string Value { get; }
public Name(string value) : base(value)
{
Value = value;
}
public static Result<Name> Create(string value)
{
if (value.Length < 3) return new ResultError("Value is not valid");
return new Name(value);
}
}
2. Using SetupSingleValueObjects()
in DbContext.OnModelCreating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.SetupSingleValueObjects(action =>
{
action.SetupMaxLength<Name>(20);
});
base.OnModelCreating(modelBuilder);
}
By following these steps, you can easily integrate and manage single-value objects in your Entity Framework Core project using the Valobtify.EFCore.SqlServer
package.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- Microsoft.EntityFrameworkCore.SqlServer (>= 9.0.0)
- Valobtify.EFCore.Core (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 87 | 12/14/2024 |