I have a Portfolio
and SampleItem
classes like this:
public class Portfolio
{
[Required]
public Guid Id { get; set; }
public virtual List<SampleItem > SampleItems { get; set; }
[NotMapped]
public List<SampleItem> Sample{
get{
return SampleItems;
}
}
}
public class SampleItems
{
[Key]
public Guid Id { get; set; }
public string Code { get; set; }
public string Type { get; set; }
}
When i upgraded project to .NET Core 3.1, an exception"
'The relationship from 'SampleItem' to 'Portfolio.SampleItems' is not supported because the owned entity type 'Portfolio' cannot be on the principal side of a non-ownership relationship.'
Portfolio is also defined as owned entity type in another class like this:
public class Bill
{
[Required(ErrorMessage = "GenId is required")]
public string GenId { get; set; }
public Guid PortfolioId { get; set; }
public virtual Portfolio Portfolio { get; set; }
}
And in my repository context class i have like this:
public virtual DbSet<Bill> Bills{ get; set; }
public virtual DbSet<Portfolio> Portfolios { get; set; }
I tried so many ways to solve the issue by defining ownsmany
in repository context class like this:
modelBuilder.Entity<Portfolio>().OwnsMany(x => x.SampleItems);
but then i get a different exception:
'The type 'Portfolio' cannot be configured as non-owned because an owned entity type with the same name already exists.'
I cannot figure out how to solve this issue with EF Core 3.1 and we cannot rollback to .NET Core 2.x
Please help me on what i m doing wrong.
Portfolio is never configured as owned type. It is referenced in Bill class through foreign key relationship.
Here is the entire repositorycontext class
public class RepositoryContext : DbContext
{
public RepositoryContext()
{
}
public RepositoryContext(DbContextOptions<RepositoryContext> options, IHttpContextAccessor httpContextAccessor) : base(options)
{
_httpContextAccessor = httpContextAccessor;
}
private readonly IHttpContextAccessor _httpContextAccessor;
public virtual DbSet<Portfolio> Portfolios { get; set; }
public virtual DbSet<Classification> Classifications { get; set; }
public virtual DbSet<Bill> Bills { get; set; }
public virtual DbSet<IntegrationEventLogEntry> IntegrationEventLogs { get; set; }
public virtual DbSet<LivingExpenseSetting> LivingExpenseSettings { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new IntegrationEventLogEntityTypeConfiguration()); //IMPOPRTANT FOR LOGGING
var status = BillStatus.Pending.ToString();
modelBuilder.Entity<Bill>().Property(lst => lst.Created).HasDefaultValueSql("getdate()");
modelBuilder.Entity<Bill>()
.Property(p => p.Status)
.HasDefaultValue("Pending");
modelBuilder.Entity<LivingExpenseSetting>();
}
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
OnBeforeSaving();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
}