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

Cannot create composite key from class #16450

Open
Tracked by #240
sam-wheat opened this issue Jul 4, 2019 · 1 comment
Open
Tracked by #240

Cannot create composite key from class #16450

sam-wheat opened this issue Jul 4, 2019 · 1 comment

Comments

@sam-wheat
Copy link

sam-wheat commented Jul 4, 2019

Many of the classes in my project have a PK composed of an int and a string property. I would like to move those two properties to their own class and use that class as the key on my entities:

Identifier:

    public class NativeIdentifier
    {
        public int DataProviderID { get; set; }
        public string NativeID { get; set; }

    }

Entities:

    public class Category
    {
        public NativeIdentifier NativeID { get; set; }
        public string Name { get; set; }

        public virtual DataProvider DataProvider { get; set; }
        public virtual ICollection<SeriesCategory> SeriesCategories { get; set; }
    }

    public class Series
    {
        public NativeIdentifier NativeID { get; set; }
        public string Name { get; set; }

        public DataProvider DataProvider { get; set; }
        public virtual ICollection<SeriesCategory> SeriesCategories { get; set; }
    }

    public class SeriesCategory
    {
        public NativeIdentifier SeriesNativeID { get; set; }
        public NativeIdentifier CategoryNativeID { get; set; }

        public virtual Series Series { get; set; }
        public virtual Category Category { get; set; }
    }

The entity I am having a problem with is SeriesCategory which simply joins Series and Category.

I try to define the PK as shown below. I need to name the properties because there are duplicate names:

    mb.Entity<SeriesCategory>().HasKey(x => new { CatDPID = x.CategoryNativeID.DataProviderID, CatNativeID = x.CategoryNativeID.NativeID, SeriesDPID = x.SeriesNativeID.DataProviderID, SeriesNativeID = x.SeriesNativeID.NativeID });

I get this error:

The properties expression 'x => new <>f__AnonymousType0`4(DataProviderID = x.CategoryNativeID.DataProviderID, NativeID = x.CategoryNativeID.NativeID, SeriesDPID = x.SeriesNativeID.DataProviderID, SeriesNativeID = x.SeriesNativeID.NativeID)' is not valid. The expression should represent a simple property access: 't => t.MyProperty'. When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'.
Parameter name: propertyAccessExpression

Is there a way to accomplish this?

@sam-wheat sam-wheat changed the title Cannot create key from inherited class Cannot create composite key from class Jul 4, 2019
@ajcvickers
Copy link
Member

Note for triage: this should work once #13947 is implemented, but we may want to keep this open to track the specific case of using this for a key.

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

3 participants