You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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.
publicclassChangeLog{publicintChangeLogId{get;set;}publicstringDescription{get;set;}publicAuditAudit{get;set;}publicAuditExtendedAuditExtended{get;set;}}[Owned]publicclassAudit{publicstringChangedBy{get;set;}publicboolIsDeleted{get;set;}[NotMapped]publicAuditExtendedAuditExtended{get;set;}}[Owned]publicclassAuditExtended{[Required][Column("Audit_AuditExtended_CreatedBy")]// optional for keeping column name as Nested typepublicstringCreatedBy{get;set;}}
foreach(var changeLog in changeLogs)// setting Direct Owned Type form Nested one{
changeLog.AuditExtended = changeLog.Audit.AuditExtended
}
context.BulkInsert(changeLogs);
I am trying to bulk insert entities with a nested owned type.
For example this slighly modified hierarchy taken from the test project:
This fails because the it tries to insert null for the AuditExtended.CreatedBy property. Any chance of adding support for nested owned types?
The text was updated successfully, but these errors were encountered: