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.

Using ValueOf with Entity Framework Core?

See original GitHub issue

Hi, I’m trying to incorporate your excellent library into my EFCore model classes, for example:

    public partial class Account
    {
        public Account()
        {
            WorkerGroups = new HashSet<WorkerGroup>();
        }

        public LongId Id { get; set; }
        public DateTime? Created { get; set; }
        public DateTime? AgreedToTermsOn { get; set; }
        public NameLabel Name { get; set; }
        public GuidStr ApiKey { get; set; }
        public string CustomUrl { get; set; }

        public virtual ICollection<WorkerGroup> WorkerGroups { get; set; }
    }

Where LongId, NameLabel, GuidStr have C# primitive types of long, string, string respectively, with appropriate validation checks (e.g. under a certain length for NameLabel, and of certain format for GuidStr).

The code compiles fine, however when I actually try to fetch one of the above record from the database, I’m running into this error: System.InvalidOperationException: The property 'Account.Id' is of type 'LongId' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

I also tried to annotate the model with something like [Column(TypeName = "bigint")] on the Id property for example, and it didn’t do anything. So I just wanted to ask you if what I’m trying to do is reasonable and/or feasible. Maybe I should just return to using primitive C# types?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
jlauwerscommented, Jun 3, 2022

If you want a generic ValueOfConverter:

public class ValueOfConverter<T, TType> : ValueConverter<T, TType>
    where T : ValueOf<TType, T>, new()
{
    public ValueOfConverter() : base(
        id => id.Value,
        id => ValueOf<TType, T>.From(id))
    {
    }
}

builder.Property(x => x.Foobar).HasConversion<ValueOfConverter<Foobar, int>>();

2reactions
SteveDunncommented, Apr 4, 2022

Vogen is similar to this but it has certain opinions/constraints on safety (for instance, not being able to create default instances of value objects). It’s also focused on speed; there’s almost no overheard compared to using a primitive directly. It’s a source generator, and can generate code for EF (as well as Json and Dapper). I’m not after stealing customers away from this very useful little package, but you might find it has what you need (if you’re happy with the constraints it imposes)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Value Objects with Entity Framework Core
Configuring Entities to Use Value Objects​​ Owned types are defined when you configure a navigation property to a particular type using the ...
Read more >
Implementing value objects
In terms of implementation, you can have a value object base class that has basic utility methods like equality based on the comparison...
Read more >
Value Objects and Their Usage with Entity Framework
Value objects gain meaning when they are used by an entity. Let's imagine user, order or shipment entities. Along with these entitties, the...
Read more >
How to configure value object for Entity Framework Core?
I am trying to create migration but don't not how to configure an Entity Framework to work with property with custom type. ......
Read more >
Value Converters in EFCore - SchwabenCode.com
In EF Core Value Conversions act as a bridge between the .NET types used in your application and the corresponding database types.
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