-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Conversation
@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
|
src/EFCore/Storage/ValueConversion/IPAddressToBytesConverter.cs
Outdated
Show resolved
Hide resolved
src/EFCore/Storage/ValueConversion/Internal/StringIPAddressConverter.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
src/EFCore/Storage/ValueConversion/IPAddressToBytesConverter.cs
Outdated
Show resolved
Hide resolved
@ajcvickers |
49b83a7
to
9045b17
Compare
@ajcvickers is here some links that you can get IPv6 https://www.mediawiki.org/wiki/Help:Range_blocks/IPv6 |
@ajcvickers, |
Thanks @ralmsdeveloper! |
Resolve: #18662