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                
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="Valobtify.EFCore.SqlServer" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Valobtify.EFCore.SqlServer --version 1.0.0                
#r "nuget: Valobtify.EFCore.SqlServer, 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.
// 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                

NuGet Package

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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