-
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
[Question] Single property owned entity with conversion not marked as owned entity #20476
Comments
HasConversion doesn't create an owned entity. I'm curious as to what let you to believe it does. |
@ajcvickers I do partially agree, that's the reason why I marked this as a question.
I think the Price entity posted above satisfies all the requirements to be called an owned entity.
calling |
I don't follow what you are partially agreeing to. Owned types in EF Core are a specific kind of entity type. Using HasConversion does not create an entity type at all. They can be used for some similar things, but they are entirely different concepts and constructs.
Please post a small, runnable project that reproduces this so we can investigate. It may be related to #20446 |
It was a poor choice of words, sorry
My wish is either to not get the
It's there and there in the first post and there's also a link to a github repo, do I need to add something more? |
@ilmax It makes no sense to use a value conversion (means the target is not an entity type) together with Owned (means that the target is an entity type). If you're doing both of these things, then no repro is needed--but don't do that. If Owned doesn't work when not combined with a value conversion, then please post a repro for this. |
@ajcvickers following your reasoning
you do not expect the class configured with |
@ilmax Correct. At least once the model is fully built. |
Cool, me neither but this code still prints the Price entity which I found a bit strange class Program
{
static void Main(string[] args)
{
var c = new Context();
_ = c.Model;
_ = Console.ReadLine();
}
}
public class Order
{
public int Id { get; set; }
public List<OrderLine> Lines { get; set; }
}
public class OrderLine
{
public int Id { get; set; }
public int Quantity { get; set; }
public Price Price { get; set; }
}
public class Price
{
public Price(int amountInCent) => AmountInCent = amountInCent;
public int AmountInCent { get; private set; }
[NotMapped]
public decimal Amount => AmountInCent / 100m;
}
public class Context : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer("whatever");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>();
modelBuilder.Entity<OrderLine>(builder =>
{
builder.Property(o => o.Price).HasColumnName("PriceInCent").HasConversion(p => p.AmountInCent, i => new Price(i));
});
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
Console.WriteLine($"Name: {entity.Name,-30} Owned: {entity.IsOwned(),-5} Keyless: {entity.IsKeyless}");
}
}
} output:
|
Model is not fully built at that point. Price is added as entityType by convention before it was configured as a property. ModelCleanupConvention cleans it up when model is fully built. |
@smitpatel thanks, make sense, so last question: What's the proper hook to add shadow properties to all the model entity? Is there a hook after ModelCleanupConvention has run? |
Thank you @AndriySvyryd, closing |
In EF core there are 2 ways of mapping owned entities sharing the same table composed of only one property. on is tu use the
OwnsOne
and another one is to useHasConversion
what I found unexpected was that using the latter doesn't make the entity as owned, even more strange is that explicitly marking the entity as owned via themodelBuilder.Owned<{Value object class}>();
still doesn't mark it as owned. full code is here:this outputs
While I was expecting this to display for the last one:
Name: EfCoreInferOwnedType.Price Owned: **true** Keyless: **true**
Why in this case the Price class is not marked as owned?
full repo here
Further technical details
EF Core version: 3.1.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Win 10
e.g. Visual Studio 2019 16.5
The text was updated successfully, but these errors were encountered: