-
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 or PhysicalAddress #18662
Comments
@tfadler This is because the in-memory provider doesn't handle these types by default. A workaround is to add value converters when using the in-memory database: modelBuilder
.Entity<NetworkStuff>()
.Property(e => e.IpAddress1)
.HasConversion(
v => v.ToString(),
v => IPAddress.Parse(v));
modelBuilder
.Entity<NetworkStuff>()
.Property(e => e.Mac1)
.HasConversion(
v => v.ToString(),
v => PhysicalAddress.Parse(v)); I'm putting this issue on the backlog to make these (or similar) converters built-in so the conversion is done automatically for any provider that doesn't support these types natively. |
@ajcvickers Yes that would be a good work around and is for just in memory database. I was hoping to have an in memory database for some Integration Tests. My boss will likely want to still have those be native type in the actual (postgresql) database. So I can't put the conversions in my OnModelCreating() for my dbcontext. I guess I could write a new dbcontext class (for ITs) that inherits my current one...so I can have access to the OnModelCreating() to put the conversions in....unless you know of another hook I can get access to modelBuilder with an Integration Test? |
@ajcvickers, I'll take this for me! |
@tfadler IModelSource can be implemented to decouple OnModelCreating from the DbContext instance. |
@tfadler You can add conditional code to OnModelCreating: if (Database.IsInMemory())
{
modelBuilder
.Entity<NetworkStuff>()
.Property(e => e.Mac1)
.HasConversion(
v => v.ToString(),
v => PhysicalAddress.Parse(v));
} |
Add built-in value converters for IPAddress Fixes #18662
Re-opening this as I hit issues as soon as I tried to create an example. Specifically, the column is created as nvarchar(max) by default: CREATE TABLE [Users] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) COLLATE German_PhoneBook_CI_AS NULL,
[HomeAddress] nvarchar(max) NULL,
CONSTRAINT [PK_Users] PRIMARY KEY ([Id])
); Also, a query like this: context.Users.Where(e => e.HomeAddress == IPAddress.Loopback).ToList(); throws:
|
Split out "Null TypeMapping in Sql Tree" to #21159 - it's a problem specific with the static readonly types on IPAddress. I don't believe it's a fundamental problem with our new IPAddress support - there's an easy workaround, we can probably deal with it separately (and even punt). #21161 adds a size hint of 45 for the string representation of IPAddress. Aside from all that, leaving this open until #20917 is merged. |
Also you can override public class TestModelCustomizer : ModelCustomizer
{
public TestModelCustomizer (ModelCustomizerDependencies dependencies)
: base(dependencies)
{
}
public override void Customize(ModelBuilder modelBuilder, DbContext context)
{
base.Customize(modelBuilder, context);
modelBuilder
.Entity<NetworkStuff>()
.Property(e => e.Mac1)
.HasConversion(
v => v.ToString(),
v => PhysicalAddress.Parse(v));
}
} And then the only thing you need is the correct registration: services.AddDbContext<FooDbContext>(builder =>
{
builder.ReplaceService<IModelCustomizer, TestModelCustomizer>();
builder.UseInMemoryDatabase();
}); |
If I have either an IPAddress or PhysicalAddress property on an entity and set it up for UseInMemoryDatabase() there will be exceptions when the database is created.
Note that these data types are acceptable in my real database (postgresql)
Thought it may be related to this: #11074
Steps to reproduce
Here is a sample application displaying the problem
Exception for IPAddress:
Exception for PhysicalAddress (have to comment out IPAddress property)
Further technical details
EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.6
The text was updated successfully, but these errors were encountered: