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

Support for nested owned types #167

Closed
dweggemans opened this issue Mar 25, 2019 · 1 comment
Closed

Support for nested owned types #167

dweggemans opened this issue Mar 25, 2019 · 1 comment

Comments

@dweggemans
Copy link

I am trying to bulk insert entities with a nested owned type.

For example this slighly modified hierarchy taken from the test project:

public class ChangeLog
{
    public int ChangeLogId { get; set; }

    public string Description { get; set; }

    public Audit Audit { get; set; }
}

[Owned]
public class Audit
{
    [Column(nameof(ChangedBy))] // for setting custom column name, in this case prefix OwnedType_ ('Audit_') removed, so column would be only ('ChangedBy')
    public string ChangedBy { get; set; } // default Column name for Property of OwnedType is OwnedType_Property ('Audit_ChangedBy')

    public bool IsDeleted { get; set; }

    [NotMapped] // alternatively in OnModelCreating(): modelBuilder.Entity<Audit>().Ignore(a => a.ChangedTime);
    public DateTime? ChangedTime { get; set; }

    // Added owned type property
    public AuditExtended AuditExtended { get; set; }
}

[Owned]
public class AuditExtended
{
    [Required] // Added required so if fails on null insert
    public string CreatedBy { get; set; }

    [NotMapped]
    public DateTime? CreatedTime { get; set; }

    [NotMapped]
    public string Remark { get; set; }
}

This fails because the it tries to insert null for the AuditExtended.CreatedBy property. Any chance of adding support for nested owned types?

@borisdj
Copy link
Owner

borisdj commented Mar 26, 2019

At the moment only first level of Composition is supported.
But you could make a workaround, by putting AuditExtended directly in Entity, since in both cases they are mapped to same table.
If you need to keep current structure in Entity model then leave the nested type but mark it with [NotMapped] attribute, and add the same owned type in Entity.
Also should you want to keep the same column naming then explicitly define it.

public class ChangeLog
{
    public int ChangeLogId { get; set; }

    public string Description { get; set; }

    public Audit Audit { get; set; }

    public AuditExtended AuditExtended { get; set; }
}

[Owned]
public class Audit
{
    public string ChangedBy { get; set; }

    public bool IsDeleted { get; set; }
    
    [NotMapped]
    public AuditExtended AuditExtended { get; set; }
}

[Owned]
public class AuditExtended
{
    [Required]
    [Column("Audit_AuditExtended_CreatedBy")] // optional for keeping column name as Nested type
    public string CreatedBy { get; set; }
}
foreach(var changeLog in changeLogs) // setting Direct Owned Type form Nested one
{
    changeLog.AuditExtended = changeLog.Audit.AuditExtended
}
context.BulkInsert(changeLogs);

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

2 participants