question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it easier to use semantic datatypes with EF core.

See original GitHub issue

A semantic data type is a struct or class with a single member of a primitive type. For example:

struct Email
{
   public string Value {get; set;}
   // other code, but no other public properties or fields
}

I use semantic data types to prevent stupid programming errors like assigning an email address to a user code or assigning a employeeId to a studentId. I get the errors at compile time.

EmployeeId employeeId = 3;
StudentId studentId = employeeId ; // gives compiler error

int employeeId = 3;
int studentId = employeeId ; // compiles but give an error at run-time or causes unexpected behavior.

However for each data type I have to make a bit of plumbing code to make it work with EF core and with Json.

I want to get rid of or the plumbing code perhaps using an extra data annotation or extra function in the context:

[UseSingleValue]
struct Email
{
   public string Value {get; set;}
}

or (but not preferred by me)

entity.Property(e => e.Email).UseSingleValue<Email>(v => v.Value)

So EF core knows that a property of type Email really is a string stored in property Value and generates the plumbing code it self.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lakemancommented, Jun 9, 2021

You could visit every property in your OnModelCreating, eg modelBuilder.Model.GetEntityTypes().Select(t => t.GetProperties()) and create a converter based on reflection information.

0reactions
rojicommented, Jun 10, 2021

Seems like the rest of this is about value objects, so we can probably close as a dup of #9906.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core - How to Configure Column Type depending on ...
I want to configure the column types depending on the database provider used. For example: public class MyClass { public Guid Id {...
Read more >
Part 5, Razor Pages with EF Core in ASP.NET Core - Data ...
The Contoso University web app demonstrates how to create Razor Pages web apps using EF Core and Visual Studio. For information about the ......
Read more >
Entity Framework Core: User-defined Fields and Tables
Using dynamically created fields or even entities in raw SQL shouldn't be a problem, but it gets more challenging when working with Entity...
Read more >
Data types and semantic types | Community Connectors
In addition to data types, Looker Studio also makes use of semantic types. Semantic types help to describe the kind of information the...
Read more >
Sato: Contextual Semantic Type Detection in Tables
Sato achieves this high prediction accuracy using a novel hybrid model that regulates semantic type pre- diction using signals from the global context...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found