'How do you find or know where the "non-owned" entity type when trying to create a migration?
I have the following classes:
JobSeeker which owns a CreditCard which has a CreditCardType
public class JobSeeker : Entity { private readonly List<CreditCard> _creditCards; public IEnumerable<CreditCard> CreditCards => _creditCards.AsReadOnly(); } public class CreditCard : Entity { public CreditCardType CardType { get { return CreditCardType.From(_creditCardTypeID); } private set { } } private readonly int _creditCardTypeID;} public class CreditCardType : Enumeration { public static readonly CreditCardType Amex = new CreditCardType(1, nameof(Amex).ToLowerInvariant()); public static readonly CreditCardType Visa = new CreditCardType(2, nameof(Visa).ToLowerInvariant()); public static readonly CreditCardType MasterCard = new CreditCardType(3, nameof(MasterCard).ToLowerInvariant()); public static IEnumerable<CreditCardType> List() => new[] { Amex, Visa, MasterCard };}
My DBContext Configs are:
class JobSeekerEntityTypeConfiguration : IEntityTypeConfiguration<JobSeeker>
{
public void Configure(EntityTypeBuilder<JobSeeker> jsConfiguration)
{
if (jsConfiguration == null)
{
throw new ArgumentNullException(nameof(jsConfiguration));
}
// Build the model
jsConfiguration.OwnsOne(s => s.CompleteName);
jsConfiguration.OwnsOne(s => s.HomeAddress);
jsConfiguration.OwnsOne(s => s.BillingAddress);
jsConfiguration.OwnsOne(s => s.EmAddress);
jsConfiguration.OwnsOne(s => s.PersonalPhoneNumber);
jsConfiguration.OwnsMany(a => a.CreditCards);
//jsConfiguration.HasMany<CreditCard>().WithOne(JobSeeker).OnDelete(DeleteBehavior.Restrict);
jsConfiguration.Property<DateTime>("CreatedDate");
jsConfiguration.Property<DateTime>("UpdatedDate");
}
}
class CreditCardTypeEntityTypeConfiguration : IEntityTypeConfiguration<CreditCard>
{
public void Configure(EntityTypeBuilder<CreditCard> ccConfiguration)
{
if (ccConfiguration == null)
{
throw new ArgumentNullException(nameof(ccConfiguration));
}
// Build the model
ccConfiguration.HasOne(o => o.CardType).WithMany().HasForeignKey("_creditCardTypeID");
ccConfiguration.Property<DateTime>("CreatedDate");
ccConfiguration.Property<DateTime>("UpdatedDate");
}
}
class CreditCardEntityTypeConfiguration : IEntityTypeConfiguration<CreditCardType>
{
public void Configure(EntityTypeBuilder<CreditCardType> cctConfiguration)
{
if (cctConfiguration == null)
{
throw new ArgumentNullException(nameof(cctConfiguration));
}
// Build the model
cctConfiguration.ToTable("CreditCardTypes");
cctConfiguration.HasKey(o => o.Id);
cctConfiguration.Property(o => o.Id)
.HasDefaultValue(1)
.ValueGeneratedNever()
.IsRequired();
cctConfiguration.Property(o => o.Name)
.HasMaxLength(200)
.IsRequired();
cctConfiguration.HasData(
new { Id = 1, Name = "Amex" },
new { Id = 2, Name = "Visa" },
new { Id = 3, Name = "MasterCard" });
}
}
My DB Context is:
public class JobSeekerContext : DbContext, IUnitOfWork
{
private static readonly Type[] EnumerationTypes = { typeof(CreditCardType) };
public const string DEFAULT_SCHEMA = "jobseeker";
private readonly ILoggerFactory MyConsoleLoggerFactory;
private readonly IMediator Mediator;
public DbSet<JobSeeker> JobSeekers { get; set; }
public DbSet<CreditCard> CreditCards { get; set; }
public DbSet<CreditCardType> CreditCardTypes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException(nameof(modelBuilder));
}
// Build the model
modelBuilder.ApplyConfiguration(new CreditCardTypeEntityTypeConfiguration());
modelBuilder.ApplyConfiguration(new CreditCardEntityTypeConfiguration());
modelBuilder.ApplyConfiguration(new JobSeekerEntityTypeConfiguration());
}
When I run the migration I get the following error: "The type 'CreditCard' cannot be marked as owned because a non-owned entity type with the same name already exists."
Where is CreditCard marked as non-owned?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
