“EntityType has no key defined” exception although key is defined with HasKey

痞子三分冷 提交于 2019-12-07 02:48:37

问题


Using EF 5 (reverse engineered code first), my model was working fine until it suddenly stopped.

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'ProjectsDate' has no key defined. Define the key for this EntityType.

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'ProjectsRisk' has no key defined. Define the key for this EntityType.

I define a key using fluent API rather than attributes, here is my ProjectsDates classes.

public partial class ProjectsDate
{
    public string OSProjectCode { get; set; }
    public Nullable<System.DateTime> TargetStart { get; set; }
    public Nullable<System.DateTime> EndDateOriginal { get; set; }
    public Nullable<System.DateTime> EndDateChangeControl { get; set; }
    public Nullable<System.DateTime> EndDateActual { get; set; }
    public Nullable<System.DateTime> GoLiveAgreed { get; set; }
    public Nullable<System.DateTime> GoLiveActual { get; set; }
    public virtual Project Project { get; set; }
}
public class ProjectsDateMap : EntityTypeConfiguration<ProjectsDate>
{
    public ProjectsDateMap()
    {
        // Primary Key
        this.HasKey(t => t.OSProjectCode);

        // Properties
        this.Property(t => t.OSProjectCode)
            .IsRequired()
            .HasMaxLength(10);

        // Table & Column Mappings
        this.ToTable("ProjectsDates");
        this.Property(t => t.OSProjectCode).HasColumnName("OSProjectCode");
        this.Property(t => t.TargetStart).HasColumnName("TargetStart");
        this.Property(t => t.EndDateOriginal).HasColumnName("EndDateOriginal");
        this.Property(t => t.EndDateChangeControl).HasColumnName("EndDateChangeControl");
        this.Property(t => t.EndDateActual).HasColumnName("EndDateActual");
        this.Property(t => t.GoLiveAgreed).HasColumnName("GoLiveAgreed");
        this.Property(t => t.GoLiveActual).HasColumnName("GoLiveActual");

        // Relationships
        this.HasRequired(t => t.Project)
            .WithOptional(t => t.ProjectsDate);

    }
}

Why doesn't EF see my fluent API mapping?


回答1:


For some reason (probably a bug), FluentAPI needs the key to be defined in a convention way - that is - ClassName + Id, or in your case:

ProjectsDateId

This way the metadata created by EF can acknowledge the fact it is related. Very annoying but...




回答2:


You need to have a global binding or individual binding in OnModelCreating
(DbModelBuilder modelBuilder).

So it would look something like this in your context file:

public override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // global
    modelBuilder.Configurations.AddFromAssembly(GetType().Assembly);
    // individual
    modelBuilder.Configurations.Add(new ProjectsDateMap());
}



回答3:


I fixed this by re-running the reverse engineer code first from EF power tools. Seemed to work from there out, this seems to be a general problem that kicks up when your models aren't configured right. It's a shame it seems so general and just kicks up misleading errors.



来源:https://stackoverflow.com/questions/16461941/entitytype-has-no-key-defined-exception-although-key-is-defined-with-haskey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!