Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add built-in value converters for IPAddress #20679

Merged
merged 3 commits into from
Apr 26, 2020

Conversation

ralmsdeveloper
Copy link
Contributor

Resolve: #18662

@ralmsdeveloper
Copy link
Contributor Author

@ajcvickers, could you confirm if this is the best way?

I did some tests and they were satisfactory.

using System;
using System.Linq;
using System.Net;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Converters
{
    class Program
    {
        static void Main(string[] args)
        {
            using var db = new SampleContext();
            db.Database.EnsureDeleted();
            db.Database.EnsureCreated();

            db.Set<Test>().Add(new Test
            {
                IPAddressToString1 = new IPAddress(new byte[] { 255, 255, 255, 0 }),
                IPAddressToBytes1 = new IPAddress(new byte[] { 10, 10, 10, 1 }),
                IPAddressToString2 = IPAddress.Parse("255.255.255.0"),
                IPAddressToBytes2 = IPAddress.Parse("10.10.10.1")
            });
            db.SaveChanges();

            var ip = db.Set<Test>().AsNoTracking().Select(p =>
                new
                {
                    p.IPAddressToString1,
                    p.IPAddressToBytes1,
                    p.IPAddressToString2,
                    p.IPAddressToBytes2,
                }).ToArray();

            var sql = db.Database.GenerateCreateScript();

            Console.WriteLine("Hello World!");
        }
    }

    class SampleContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("..");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Test>()
                .Property(p => p.IPAddressToString1)
                .HasConversion<string>();

            modelBuilder.Entity<Test>()
                .Property(p => p.IPAddressToBytes1)
                .HasConversion<byte[]>();

            modelBuilder.Entity<Test>()
                .Property(p => p.IPAddressToString2)
                .HasConversion(new IPAddressToStringConverter());

            modelBuilder.Entity<Test>()
                .Property(p => p.IPAddressToBytes2)
                .HasConversion(new IPAddressToBytesConverter());
        }
    }

    class Test
    {
        public int Id { get; set; }
        public IPAddress IPAddressToString1 { get; set; }
        public IPAddress IPAddressToBytes1 { get; set; }
        public IPAddress IPAddressToString2 { get; set; }
        public IPAddress IPAddressToBytes2 { get; set; }
    }
}

Script:

CREATE TABLE [Test] (
    [Id] int NOT NULL IDENTITY,
    [IPAddressToString1] nvarchar(max) NULL,
    [IPAddressToBytes1] varbinary(max) NULL,
    [IPAddressToString2] nvarchar(max) NULL,
    [IPAddressToBytes2] varbinary(max) NULL,
    CONSTRAINT [PK_Test] PRIMARY KEY ([Id])
);
GO

@ajcvickers ajcvickers self-assigned this Apr 20, 2020
Copy link
Member

@ajcvickers ajcvickers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments, but in general looks good.

@ralmsdeveloper
Copy link
Contributor Author

@ajcvickers
I made some adjustments, see if it looks good to you!

@ralmsdeveloper ralmsdeveloper changed the title [WIP] Add built-in value converters for IPAddress Add built-in value converters for IPAddress Apr 22, 2020
@ralmsdeveloper
Copy link
Contributor Author

@ralmsdeveloper
Copy link
Contributor Author

@ajcvickers,
if you find the time, could you look at that? I would like to end this weekend.

@ajcvickers ajcvickers merged commit 86f2817 into dotnet:master Apr 26, 2020
@ajcvickers
Copy link
Member

Thanks @ralmsdeveloper!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add built-in value converters for IPAddress or PhysicalAddress
3 participants