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

孤街浪徒 提交于 2019-12-05 05:35:23

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...

leeeennyy

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());
}

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.

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