3

I have the following situation in my ASP.NET Core application with Entity Framework Core 1.1

Database-Table named "Content"

  • Content_ID (int not null, primary key)
  • Title (varchar max)
  • Description (varchar max)

Model ("ContentEntry.cs"):

public class ContentEntry
{
    public int Id {get; set;}
    public string Title {get; set;}
    public string Description {get; set;}
}

Configuration File (ContentEntryConfiguration.cs)

public class ContentEntryConfiguration : IEntityMappingConfiguration<ContentEntry>
{
    public void Map(EntityTypeBuilder<ContentEntry> modelBuilder)
    {
        modelBuilder.HasKey(m => m.Id);

        modelBuilder.Property(m => m.Id).HasColumnName("Content_ID");
        modelBuilder.Property(m => m.Title ).HasColumnName("Title");
        modelBuilder.Property(m => m.Description).HasColumnName("Description");

        modelBuilder.ToTable("Content");
    }
}

As mentioned above, the primary key of my table is named "Content_ID". When I execute a LINQ query, I receive an error saying that the column "ID" hasn't been found on the database. After inspecting the generated query with the SQL Profiler, I noticed that the query contains "ID" instead of "Content_ID". I expect entity framework to generate a query containing the column "Column_ID" instead and map it to my model-property named "Id".

Do you have an idea why this is happening and how I could fix this issue?

1
  • @Kris i corrected it; it was just a typing error, but not the cause of my problem.
    – dario
    Jul 10, 2017 at 15:11

2 Answers 2

1

For people with simpler needs, this will suffice

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<ContentEntry>().Property(c => c.Id).HasColumnName("Content_ID");
}
0

I solved it, I just forgot to register the entity mapping in my DB Context Class:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.RegisterEntityMapping<ContentEntry, ContentEntryConfiguration>();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.