Repository navigation
Open
Milestone
Description
I implemented the following entities:
namespace VinylStore.Catalog.Domain.Entities
{
public class Item
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string LabelName { get; set; }
public Money Price { get; set; }
public string PictureUri { get; set; }
public DateTimeOffset ReleaseDate { get; set; }
public string Format { get; set; }
public int AvailableStock { get; set; }
public Guid GenreId { get; set; }
public Genre Genre { get; set; }
public Guid ArtistId { get; set; }
public Artist Artist { get; set; }
public bool IsInactive { get; set; }
}
public class Artist
{
public Guid ArtistId { get; set; }
public string ArtistName { get; set; }
public ICollection<Item> Items { get; set; }
}
public class Genre
{
public Guid GenreId { get; set; }
public string GenreDescription { get; set; }
public ICollection<Item> Items { get; set; }
}
}
I've mapped the entities using the following schemas definitions: SchemaDefinitions.cs
When I run some in-memory tests I get the following exception:
Exception message: `System.AggregateException : One or more errors occurred. (The type 'Artist' cannot be marked as owned because a non-owned entity type with the same name already exists.) `
Stack trace: ```
System.AggregateException : One or more errors occurred. (The type 'Artist' cannot be marked as owned because a non-owned entity type with the same name already exists.) (The following constructor parameters did not have matching fixture data: CatalogDataContextFactory catalogDataContextFactory)
---- System.InvalidOperationException : The type 'Artist' cannot be marked as owned because a non-owned entity type with the same name already exists.
---- The following constructor parameters did not have matching fixture data: CatalogDataContextFactory catalogDataContextFactory
----- Inner Stack Trace #1 (System.InvalidOperationException) -----
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(TypeIdentity& type, ConfigurationSource configurationSource, Nullable`1 shouldBeOwned)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(Type type, ConfigurationSource configurationSource, Nullable`1 shouldBeOwned)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.HasOwnership(TypeIdentity& targetEntityType, PropertyIdentity navigation, Nullable`1 inverse, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.HasOwnership(Type targetEntityType, MemberInfo navigationProperty, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.OwnsOneBuilder[TRelatedEntity](PropertyIdentity navigation)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.OwnsOne[TRelatedEntity](Expression`1 navigationExpression, Action`1 buildAction)
at VinylStore.Catalog.Infrastructure.SchemaDefinitions.ItemEntitySchemaDefinition.Configure(EntityTypeBuilder`1 ```
Further technical details
EF Core version: 3.0.0-preview5.19227.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: MacOS
IDE: Rider
Reacted by Shimmy Weitzhandler, Riana, keatkeat87, seattleite7, George Petri, Jämes Ménétrey, Paulo, Richard Collette, Mateusz, Andrej Križan and 26 more
Activity
ajcvickers commentedon May 10, 2019
@samueleresca The configuration for an owned type needs to be done in the
OwnsOne/OwnsManyAPI calls of the entity type that owns it. It is not currently possible to have a separateIEntityTypeConfigurationfor an owned type. Doing so causes EF to configure it as a normal, non-owned entity type.ajcvickers commentedon May 10, 2019
Putting this on the backlog to consider patterns that allow owned type mappings to be split out like this.
samueleresca commentedon May 10, 2019
Hi @ajcvickers, thanks for the support. I've tried to convert the code using the
OwnsOneAPI, but I'm still getting the error.This is the code:
This is the exception:
samueleresca commentedon May 10, 2019
Plus, I'm not sure about the way that
OwnsOne/OwnsMany. If my domain model has a chain of three relationships, means that I will have 3 levels of nesting in my code. Something like:I think the level of nesting causes a little bit of redundancy? Is there any specific reason why you are keeping this syntax?
ajcvickers commentedon May 13, 2019
@samueleresca If you are still seeing issues, then please post a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.
With regard to the model configuration syntax, the reason for it is that it forms a complete configuration for the aggregate; that is, the 'normal' entity type as the aggregate root, and all its owned types as the aggregate children. Each aggregate can be configured differently even if it's children have the same CLR types as another aggregate's children.
That being said, we recognize that in many cases an owned type is configured the same way for many aggregates, which is why we do plan to support allowing common configuration. It's just not implemented yet.
Allann commentedon Jul 15, 2019
I too am getting this after an upgrade to 3.0.0-preview6.19304.10. The configuration was working fine in 2.2 if that counts as I have a few existing migrations that worked fine and produced the structure I was intending.
Interestingly, I do have configuration files for the owned class to define the string fields. How would you do that without them? I can't use attributes at this stage.
This must have been supported in 2.2, is this completely new again?
Happy to supply the code if needed.
ChristopherHaws commentedon Aug 17, 2019
I am having the same issue as well. I created a sample app to reproduce the issue: https://github.com/ChristopherHaws/EFCore3MigrationBug
If you delete the existing migration, create a new migration with EF Core 3.0, and compare them it looks like it is failing to migrate from:
to:
[-]The type 'Type` cannot be marked as owned because a non-owned entity type with the same name already exists.[/-][+]Add IOwnedTypeConfiguration[/+][-]Add IOwnedTypeConfiguration[/-][+]Add an IEntityTypeConfiguration equivalent for owned types[/+]weitzhandler commentedon Sep 26, 2019
Looks like things have changed in EF Core 3.0.
After upgrading, my code doesn't work.
Here's my old code:
I've updated my code to:
Now I'm getting the following exception on the
builder.Entity<Address>()call:weitzhandler commentedon Sep 26, 2019
This worked:
AndriySvyryd commentedon Oct 3, 2019
Here's a proposal:
It would be used like this:
Only apply config to entity types
GasyTek commentedon Oct 31, 2019
I think the goalis to be able to configure common properties of an owned type accross all aggregates that reference it, so this proposal miss the point here.
For instance if we have Person, Company and Address classes like this.
The Address class is a owned type of both Person and Company and for both , its Street property should be 10 characters max. Currently, we will have two configurations for both Person and Company and within each one, we must specify that the Street property should be 10 char length max.
It will be greate to be able to put those common configuration within one class file like we do in EF6 with ComplexTypeConfiguration.
smitpatel commentedon Oct 31, 2019
@GasyTek - That is being tracked in different issue. See #6787 (comment)
AndriySvyryd commentedon Nov 2, 2019
I updated the proposal with a non-generic interface that can be shared between multiple owned types.