Yvain February 2016

EF Migration Clear Cache

public class A
{
    public int Id { get; set; }

    public virtual ICollection<AHistory> AHistorys { get; set; }
}

public class AHistory
{
    public int AId { get; set; }

    public virtual A A {get; set; }
}

I renammed AHistories to AHistory.

add-migration HistoMig

AHistories: EntityType: EntitySet 'AHistories' is based on type 'AHistory' that has no keys defined.

So the error mention an old name that no longer exists in the solution.

What should I do ?

I've already clean Visual Studio Solution with no effects.

I also tried to comment out navigation property, add migration, rollback migration, uncomment then add migration ; I still get this erros.

I've done a search through VS solution on String "AHistories" with 0 occurence found.

Answers


Yvain February 2016

Based on ESG comment, I've added :

public class DefaultContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new AHistoryMapping());

        public DbSet<AHistory> AHistorys { get; set; }
    }
}

public class AHistoryMapping : EntityTypeConfiguration<AHistory>
{
    public AHistoryMapping()
    {
        HasKey(ah => ah.AId);
    }
}

It works now !

Edit

Actually it doesn't "work"... It compiles but the result is not what I was expecting.

This code creates a table with a unique identifier named FundId and a foreign key named Fund_Id.

I've change my code to

public class AHistoryMapping : EntityTypeConfiguration<AHistory>
{
    public AHistoryMapping()
    {
        HasRequired(ah => ah.A)
        .WithMany(a => a.AHistorys)
        .HasForeignKey(ah => ah.AId);
    }
}

It doesn't compile anymore. I get the message again.

AHistory: : EntityType 'AHistory' has no key defined. Define the key for this EntityType. AHistorys: EntityType: EntitySet 'AHistorys' is based on type 'AHistory' that has no keys defined.

Edit 2

It turns out that EF require a Primary Key. Here is the solution :

public class AHistoryMapping : EntityTypeConfiguration<AHistory>
{
    public AHistoryMapping()
    {
        HasKey(ah => new { ah.AId, Ah.Date });
        HasRequired(ah => ah.A)
        .WithMany(a => a.AHistorys)
        .HasForeignKey(ah => ah.AId);
    }
}

Post Status

Asked in February 2016
Viewed 3,453 times
Voted 11
Answered 1 times

Search




Leave an answer