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

Value generators don't work owned entity's properties #18339

Closed
salaros opened this issue Oct 11, 2019 · 1 comment
Closed

Value generators don't work owned entity's properties #18339

salaros opened this issue Oct 11, 2019 · 1 comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@salaros
Copy link

salaros commented Oct 11, 2019

Value generators don't work owned entity's properties

Steps to reproduce

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

	public AuditLog AuditLog { get; set; }
}

[Owned]
public class AuditLog : IHasSoftDelete
{
    public bool IsDeleted { get; set; }

    public DateTime CreatedTime { get; set; }

    public DateTime? ModifiedTime { get; set; }

    public string CreatedBy { get; set; }

    public string ModifiedBy { get; set; }
}

public class CurrentUserNameGenerator : ValueGenerator<string>
{
    public override bool GeneratesTemporaryValues => false;

    public override string Next(EntityEntry entry) => "FooBarUser";
}

dbContext
    .Entity<Person>()
    .OwnsOne(p => p.AuditLog)
    .Property(a => a.CreatedBy)
    .HasColumnName(nameof(AuditLog.CreatedBy))
    .HasValueGenerator<CurrentUserNameGenerator>() // <-- the generator is not triggered when CreatedBy is null
    .IsRequired(true); // doesn't work BTW, it's just ignored

Further technical details

EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.0
Operating system: Windows 10 1903 Pro x64 EN
IDE: Visual Studio 2019 16.3.x

@ajcvickers
Copy link
Member

Note for triage: Calling HasValueGenerator does not make the property ValueGenerated.OnAdd.

@salaros Explicitly specify that the property should get a generated value when a new entity is being attached:

modelBuilder.Entity<Person>()
    .OwnsOne(p => p.AuditLog)
    .Property(a => a.CreatedBy)
    .HasColumnName(nameof(AuditLog.CreatedBy))
    .ValueGeneratedOnAdd()
    .HasValueGenerator<CurrentUserNameGenerator>()
    .IsRequired(true);

@ajcvickers ajcvickers added this to the Backlog milestone Oct 18, 2019
@AndriySvyryd AndriySvyryd self-assigned this Mar 20, 2020
@AndriySvyryd AndriySvyryd removed their assignment Sep 1, 2020
@AndriySvyryd AndriySvyryd added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed consider-for-current-release labels Sep 1, 2020
@AndriySvyryd AndriySvyryd modified the milestones: Backlog, 5.0.0-rc2 Sep 1, 2020
AndriySvyryd added a commit that referenced this issue Sep 1, 2020
AndriySvyryd added a commit that referenced this issue Sep 1, 2020
AndriySvyryd added a commit that referenced this issue Sep 1, 2020
AndriySvyryd added a commit that referenced this issue Sep 2, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc2, 5.0.0 Nov 7, 2020
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. customer-reported type-bug
Projects
None yet
Development

No branches or pull requests

3 participants