[英]EF 5.0 Fluent mapping: “EntityType '{ClassMap}' has no key defined. Define the key for this EntityType.”
I'm using EF5 as my connection between MODEL & Database. 我正在使用EF5作为MODEL和数据库之间的连接。 I have the following layers for my solution:
我的解决方案分为以下几层:
The error I get is the following: base {System.Exception} = {"One or more validation errors were detected during model generation:\\r\\n\\r\\n\\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'AnnualPeriodMap' has no key defined. Define the key for this EntityType.\\r\\n\\tSystem.Data.Entity.Edm.EdmEntitySet: EntityTy... 我得到的错误如下: base {System.Exception} = {“在模型生成期间检测到一个或多个验证错误:\\ r \\ n \\ r \\ n \\ tSystem.Data.Entity.Edm.EdmEntityType::EntityType'未定义AnnualPeriodMap'键。为此EntityType定义键。\\ r \\ n \\ tSystem.Data.Entity.Edm.EdmEntitySet:EntityTy ...
My model classes are as basic as I can, but to help myself from typing redundant code, some of the model classes inherit from a class I called EntityBase , which simply holds the Id property. 我的模型类尽可能地基础,但是为了帮助自己避免键入冗余代码,一些模型类继承自我称为EntityBase的类,该类仅持有Id属性。 Below is the code for this class:
下面是该类的代码:
/// <summary>
/// Base implementation for IEntity.
/// </summary>
/// <typeparam name="TId">The type of the id.</typeparam>
public abstract class EntityBase<TId> : IEntity<TId>
{
public virtual TId Id { get; set; }
object IEntity.Id
{
get
{
return this.Id;
}
set
{
this.Id = (TId)value;
}
}
}
EntityBase itself inherits from IEntity , which is defined in Company.Common.Data, here's the code for this interface: EntityBase本身继承自Company.Common.Data中定义的IEntity ,这是此接口的代码:
public interface IEntity
{
/// <summary>
/// The entity's id.
/// </summary>
object Id { get; set; }
}
/// <summary>
/// Defines an entity.
/// </summary>
/// <typeparam name="TKey">The type of the entity's key.</typeparam>
public interface IEntity<TKey> : IEntity
{
/// <summary>
/// The entity's id.
/// </summary>
new TKey Id { get; set; }
}
I did all of this to make my life easier when having to declare model entities that share same old data, such as Id's... Below is the code of a model class i'm experiencing issues with at the moment, namely AnnualPeriod , which as you can see inherits from EntityBase (of long) . 当需要声明共享相同旧数据的模型实体(例如Id的实体)时,我做了所有这些事情,从而使我的生活更加轻松。下面是我目前遇到问题的模型类的代码,即AnnualPeriod ,如您所见,继承自EntityBase(很长) 。 EntityBase itself is shown above, the first code example...
上面显示了EntityBase本身,第一个代码示例...
public class AnnualPeriod : EntityBase<long>
{
public virtual int AnnualPeriodTypeId { get; set; }
public virtual AnnualPeriodType AnnualPeriodType { get; set; }
public virtual string Code { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual DateTime EndDate { get; set; }
public virtual ICollection<Counter> Counters { get; set; }
}
My EF mapping is done via EntityTypeConfiguration (of ModelClass) classes, an example of such a mapping below: 我的EF映射是通过EntityTypeConfiguration(属于ModelClass)类完成的,下面是此类映射的示例:
public class AnnualPeriodMap : EntityTypeConfiguration<AnnualPeriod>
{
public AnnualPeriodMap()
{
// Table
this.ToTable("AnnualPeriods");
// Primary Key
this.HasKey(e => e.Id);
// Columns
this.Property(e => e.Id).HasColumnName("id");
this.Property(e => e.AnnualPeriodTypeId).HasColumnName("annualPeriodTypeId");
this.Property(e => e.Code).HasColumnName("code");
this.Property(e => e.StartDate).HasColumnName("startDate");
this.Property(e => e.EndDate).HasColumnName("endDate");
}
}
Now when executing the code, I experience that EF5 says that EntityType 'AnnualPeriodMap' has no key defined. 现在,在执行代码时,我体验到EF5表示EntityType'AnnualPeriodMap'没有定义键。 Define the key for this EntityType .
定义此EntityType的键 。 I don't understand what I'm doing wrong, as I cleary stated the code this.HasKey(e => e.Id) in my AnnualPeriodMap (EntityTypeConfiguration) class.
我不明白我在做什么错,因为我清楚地在我的AnnualPeriodMap (EntityTypeConfiguration)类中声明了代码this.HasKey(e => e.Id) 。
It's obvious I'm doing something wrong, in either configuration, or in not understanding how EF works properly. 很明显,我在配置上或者在不了解EF如何正常工作方面都做错了。 But I haven't found any information about setting up constructions like this on the internet...
但是我还没有在互联网上找到关于建立这样的建筑的任何信息...
Thanks in advance for your advice, Yves Schelpe 预先感谢您的建议,Yves Schelpe
It seems like there will be a way around this in EF6 with Custom Conventions. 似乎在EF6中使用自定义约定可以解决此问题。 See [ http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-B335#fbid=yJPWH45LEuw ] (at around 00:20:00 of the talk).
请参阅[ http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-B335#fbid=yJPWH45LEuw ](在演讲的00:20:00左右)。
Please mention Key attribute 请提及密钥属性
namespace:using using System.ComponentModel.DataAnnotations; 名称空间:使用System.ComponentModel.DataAnnotations使用;
public class AnnualPeriod : EntityBase<long>
{
[Key]
public virtual int AnnualPeriodTypeId { get; set; }
public virtual AnnualPeriodType AnnualPeriodType { get; set; }
public virtual string Code { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual DateTime EndDate { get; set; }
public virtual ICollection<Counter> Counters { get; set; }
}
For me the solution was to add the EntityTypeConfiguration into the DbContext initialization. 对我来说,解决方案是将EntityTypeConfiguration添加到DbContext初始化中。 I had declared correctly the DbSet but for an unknown reason I totally forgot to configure it!
我已经正确声明了DbSet,但是由于未知的原因,我完全忘记了对其进行配置! Something like:
就像是:
public IDbSet<AnnualPeriod> AnnualPeriods { get; set; }
void OnModelCreating(DbModelBuilder modelBuilder){
....
//I was missing this line
modelBuilder.Configurations.Add(new AnnualPeriodMap ());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.