Skip to content
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

Create haskey with nested owned types #17995

Closed
ivanrodriguezfernandez opened this issue Sep 23, 2019 · 2 comments
Closed

Create haskey with nested owned types #17995

ivanrodriguezfernandez opened this issue Sep 23, 2019 · 2 comments

Comments

@ivanrodriguezfernandez
Copy link

ivanrodriguezfernandez commented Sep 23, 2019

I want create a primary key with all properties in value object

I implemented the following entities:

public class Dashboard : Entity
    {
        public Dashboard()
        {
            DashboardItems = new List<DashboardItem>();
        }

        public string Name { get; private set; }
        public List<DashboardItem> DashboardItems { get; private set; }

        public Dashboard(string name, List<DashboardItem> dashboardItems)
        {
            Name = name;
            DashboardItems = dashboardItems;
        }

        public void Update(string name, List<DashboardItem> dashboardItems)
        {
            Name = name;
            DashboardItems = dashboardItems;
        }
    }

 public class DashboardItem : ValueObject
    {
        public DashboardItem()
        {

        }
        public DashboardItem(string appViewId, Size size, Position position)
        {
            AppViewId = appViewId;
            Size = size ?? new Size(1, 1);
            Position = position ?? new Position(0, 0);
        }

        public string AppViewId { get; private set; }
        public Size Size { get; private set; }
        public Position Position { get; private set; }
        protected override IEnumerable<object> GetAtomicValues()
        {
            yield return AppViewId;
            yield return Position.X;
            yield return Position.Y;
            yield return Size.Width;
            yield return Size.Height;
        }
    }


  public class Position : ValueObject
    {
        public Position(int x = 0, int y = 0)
        {
            X = x;
            Y = y;
        }

        public int X { get; private set; }

        public int Y { get; private set; }

        protected override IEnumerable<object> GetAtomicValues()
        {
            yield return X;
            yield return Y;
        }
    }
public class Size : ValueObject
    {

        public Size(int width = 0, int height = 0)
        {
            Width = width;
            Height = height;
        }

        public int Width { get; private set; }

        public int Height { get; private set; }

        protected override IEnumerable<object> GetAtomicValues()
        {
            yield return Width;
            yield return Height;
        }
    }

With this configuration

modelBuilder.Entity<Dashboard>().HasKey(o => o.Id);

            modelBuilder.Entity<Dashboard>().OwnsMany(p => p.DashboardItems, dashboardItemsBuilder =>
            {
                dashboardItemsBuilder.HasForeignKey("DashboardId");

                dashboardItemsBuilder.OwnsOne(x => x.Size);
                dashboardItemsBuilder.OwnsOne(x => x.Position);

                dashboardItemsBuilder.HasKey("DashboardId", "AppViewId", "Size_Height", "Size_Width", "Position_X", "Position_Y");
            });

This configuration generate following exception

System.InvalidOperationException: The property 'Size_Height' cannot be added to the type 'DashboardItem' because there was no property type specified and there is no corresponding CLR property or field. To add a shadow state property the property type must be specified.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.GetOrCreateProperties(IReadOnlyList`1 propertyNames, Nullable`1 configurationSource, IReadOnlyList`1 referencedProperties, Boolean required, Boolean useDefaultType)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList`1 propertyNames, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.CollectionOwnershipBuilder.HasKey(String[] propertyNames)

I pushed this example in
https://github.com/ivanrodriguezfernandez/ef-core-owned-types

Further technical details

EF Core version: 2.2.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 2.2
Operating system:
IDE: Visual Studio 2019 16.2.5

@smitpatel
Copy link
Contributor

@AndriySvyryd - Can you find dupe of this?

@AndriySvyryd
Copy link
Member

Duplicate of #13566

@AndriySvyryd AndriySvyryd marked this as a duplicate of #13566 Sep 27, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants