-
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
Support for custom type mapping and data store to CLR type conversions #242
Comments
Just checking - Is this the work item tracking this uservoice item? Thanks. |
Any movement on this? Or hint of direction, proposed API, anything? Thanks. |
@mj1856 nothing yet. We know we want to do this (and that has influenced how things are architected internally) but we're currently planning to work on lighting this feature up after our initial RTM of EF7. |
Thanks! |
Details: |
Does this allow me to map a custom type to whatever filed in the storage? e.g. create a custom type for entity.Id (which only wraps an int) instead of using int or map enum to char? |
This is exactly what I was recently looking to do. It would be an extremely useful to have this. |
I need the same possibility as mentioned by @ilmax:
Does this allow me to do that? |
@glucaci Looks like a bug--can you file a new issue for it? |
Sure, I've just created one #10765 I don't know how the conversion should work internally but I realize that by ignoring the property everything is working (column created, saving to db, queering the db), but it seams that it's not the proper way 😏 |
Note for triage: Additional work to be done here:
|
When this was first created back in 2014 (based on a uservoice suggestion from 2012) the intent was obviously for it to be a feature of legacy EF(ie.1-6). So while it is great that EFCore supports this, where does this leave the vast majority of us with existing apps on EF6 (that can't update until EFCore is at feature parity with EF6 - ie. GROUP BY!) Is there any plans to backport this to EF6? |
@tystol This issue is not something that our team is planning to address in the EF6.x code base. This does not mean that we would not consider a community contribution to address this issue. However, the nature of the EF6 code and the EDM type system makes it non-trivial to implement. Being able to do things like this in a reasonable way is one of the reasons for EF Core being a new codebase that is not a front for EDM. |
As already mentioned from @Marusyk and @ilmax:
I already tried ValueConverters and those are awesome features! But today i tried to use a custom type for Identities (primary and foreign keys). In DDD where you want to express your values with more context - an int (primary key and foreign key) is just an int and its value could be anything. It would be so much value for EF if it would be possible to consider value conversations for primary keys and foreign keys. Is something like this planned or will this issue resolve parts of this? public class CustomerId
{
public int Value { get; }
public CustomerId(int value)
{
if (value <= 0) throw new ArgumentOutOfRangeException(nameof(value));
Value = value;
}
}
public class Customer
{
public CustomerId Id { get; private set; }
public CustomerId ReferredById { get; private set; }
}
public class CustomerEntityConfiguration : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.ToTable("Customers");
builder.HasKey(x => x.Id);
// works already fine and maps client type CustomerId to storage type int and reverse
builder.Property(x => x.Id).HasConversation(id => id.Value, value => new CustomerId(value);
// not possible (Exception) because "Id" must be an int to support value generation
// but since there is an value conversation, it should use the storage type which is/results in an int?
builder.Property(x => x.Id).UseSqlServerIdentityColumn();
}
} |
@essmd To clarify, the only part of this that isn't working for you is that the key cannot be store-generated? |
@ajcvickers Correct! As i can see here in the code, converters are not allowed. My expectation from this setup was, that the add migration would create an migration with an Later when SaveChangesAsync is called, EF executes the I tested again with foreign key too, and its also not possible to map foreign keys with properties using custom types and configured converters: public class Order
{
public CustomerId CustomerId { get; private set; }
}
public class OrderEntityConfiguration : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.ToTable("Orders");
// works fine and maps client type CustomerId to storage type int and reverse
builder.Property(x => x.CustomerId)
.HasConversation(id => id.Value, value => new CustomerId(value);
// not working
builder.HasOne<Customer>()
.WithMany()
.HasForeignKey(x => x.CustomerId);
}
} |
There is a continuum of scenarios that can be supported here:
char
can map to the database exactly as astring
of size 1 (see Remove SqlServer TypeMapping for CLR type char #8656)byte
can map to the database exactly the same as abyte[]
They all probably require extending the reach of the type mapper to be able to participate of the generation of:
The text was updated successfully, but these errors were encountered: