Suppose I have an Contact Table in my database, with a PhoneNumber column, which is a string, "+44123456789", for example.
In my domain model, I would normally have:
public class Contact {
public int Id { get; set; }
public string PhoneNumber { get; set; }
}
So far so ordinary :) But now, I want to associate a bunch of non-DB logic with my PhoneNumber.
I can make a PhoneNumber object to hold all that logic, and make my PhoneNumber
property be a Number
object instead:
public class Contact {
public int Id { get; set; }
public Number PhoneNumber { get; set; }
}
public class Number{
public string Number { get; set; }
//... other logic, getter wrappers, etc. ...
}
But EF is going to interpret that as "Number is a new type with a separate table, and the Contact object should have a FK to a row in the Number table". Which I don't want - I want to keep the DB model the same. I can just do the conversion manually, in my C# but it's going to end up with a lot of boilerplate.
I would like to be able to tell EF, "I know this looks like an object, but trust me, it's a primitive. When you're storing it in the database, it's actually a primitive column. Please convert it to and from the primitive in the appropriate way (provided by the Number class, if necessary) whenever you interact with the database.
Does this concept exist in EF?
If so, what is the correct search term to find documentation of this?
Interested to know in either EF or EFCore.