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

ModelBuilder: Shadow property introduction doesn't work without calling Property<T>() #4843

Closed
bricelam opened this issue Mar 17, 2016 · 4 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@bricelam
Copy link
Contributor

class TestClass
{
    public string Name { get; set; }
}

class Test
{
    public int Id { get; set; }
    public TestClass Class { get; set; }
}
modelBuilder.Entity<TestClass>(
    x =>
    {
        // NOTE: HasKey fails without this
        x.Property<string>("AssemblyName");

        x.HasKey("AssemblyName", "Name");
    });
modelBuilder.Entity<Test>(
    x =>
    {
        // NOTE: HasForeignKey fails without this
        x.Property<string>("AssemblyName");

        x.HasOne(t => t.Class).WithMany(c => c.Tests)
            .HasForeignKey("AssemblyName", "ClassName");
    });
@smitpatel
Copy link
Contributor

HasKey does not create shadow properties therefore the shadow property needs to be defined first. (same with HasIndex)
HasForeignKey creates shadow properties but only when there is compatible Principal Key to replicate the property types. For composite FK, there should be either HasPrincipalKey call or principal type should have composite PK defined.
At present we only replicate the type if the referenced property is non-shadow.

@rowanmiller rowanmiller added this to the 1.0.0 milestone Mar 25, 2016
@rowanmiller rowanmiller modified the milestones: 1.0.1, 1.0.0 May 11, 2016
@divega divega changed the title ModelBuilder: Shadow property introduction doesn't work with composite shadow keys ModelBuilder: Shadow property introduction doesn't work without calling Property<T>() Jun 4, 2016
@divega
Copy link
Contributor

divega commented Jun 4, 2016

Updating title because the issue isn't specific to composite keys.

It would be nice if calling HasKey("Id") could act as a shortcut to both create the property (if it didn't already exists) and to make it a key, but HasKey() currently doesn't provide a way to specify the type.

BTW, the exception is:

The property 'Id' cannot be added to the entity type 'Customer' because there was no property type specified and there is no corresponding CLR property. To add a shadow state property the property type needs to be specified.

@smitpatel
Copy link
Contributor

One of the reasons for not creating shadow properties for HasKey & HasIndex is "typo"

@divega
Copy link
Contributor

divega commented Jun 4, 2016

@smitpatel agreed, but when using the string version of Property<T>() we already assume the user's intent is a bit more likely to be to refer to a shadow property. Then by extension I think it would be ok to assume the same thing for other string based APIs on the same EntityTypeBuilder where the property would need to be created.

In any case (unless I am missing a detail that justified the original title to be about composite keys), this is assigned to 1.0.1, so we can discuss it soon.

AndriySvyryd added a commit that referenced this issue Jun 8, 2016
Infer FK property types from shadow principal keys

Fixes #5350
Fixes #4843
AndriySvyryd added a commit that referenced this issue Jun 8, 2016
Infer FK property types from shadow principal keys

Fixes #5350
Fixes #4843
@AndriySvyryd AndriySvyryd modified the milestones: 1.0.0, 1.0.1 Jun 8, 2016
@AndriySvyryd AndriySvyryd removed their assignment Jun 8, 2016
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

6 participants