简体   繁体   English

实体框架验证错误没有定义键。 定义此EntityType的键

[英]Entity framework Validation errors has no key defined. Define the key for this EntityType

I am getting this Entity Framework error when I try to save an object in my Database context. 当我尝试在数据库上下文中保存对象时,我收到此实体框架错误。 I have no idea where to start looking. 我不知道从哪里开始寻找。

What could be happening here? 这可能发生什么?

How can I isolate what the issue is? 我怎样才能找出问题所在呢?

Thanks. 谢谢。

{System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Exception' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'AssemblyName' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'CultureInfo' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'DateTimeFormatInfo' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'IdentityReference' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Exceptions' is based on type 'Exception' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'AssemblyNames' is based on type 'AssemblyName' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'CultureInfoes' is based on type 'CultureInfo' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'DateTimeFormatInfoes' is based on type 'DateTimeFormatInfo' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'IdentityReferences' is based on type 'IdentityReference' that has no keys defined.

   at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
   at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateCsdl(EdmModel model)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
   at System.Data.Entity.DbSet`1.Add(TEntity entity)
   at MVCDaemon.Controllers.AwardBadgesController.FromUnprocessedEvents() in Hobby Stuff\Hehe\Hehe.Daemon\Controllers\AwardBadgesController.cs:line 58}

and my other stuff: 和我的其他东西:

DaemonDB.Tasks.Add(T);

and

public class DaemonDBContext : DbContext
    {
        public DaemonDBContext() : base("DefaultConnection") { }

        //Collections also in BaseDBContext:
        public DbSet<Badge> Badges { get; set; }
        public DbSet<BadgeGroup> BadgeGroups { get; set; }
        public DbSet<Event> Events { get; set; }

        //Collections only in DaemonDBContext
        public DbSet<EventCheckedForBadgeReward> EventCheckedForBadgeReward { get; set; }
        public DbSet<Task> Tasks { get; set; }
    }

and

public class Task
    {
        public int ID { get; set; }
        public string FriendlyTitle { get; set; }
        public DateTime TimeOfExecution { get; set; }
        public TimeSpan LengthOfExecution { get; set; }
        public Stopwatch StopWatch;
        public Exception ExceptionEncountered { get; set; }

        public Task() {
            TimeOfExecution = DateTime.Now;
        }

        public void StartStopwatch()
        {
            StopWatch = Stopwatch.StartNew();
            StopWatch.Start();
        }

        public void StopStopwatch()
        {
            StopWatch.Stop();
        }

        public string GetStatusAsFriendly()
        {
            return ExceptionEncountered == null ? "Success" : "Failed";
        }
    }

不得不在某些属性上使用[NotMapped]属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EntityType&#39;&#39;没有定义键。 定义此EntityType的键。 -C#Web API实体框架 - EntityType ' ' has no key defined. Define the key for this EntityType. - C# Web API Entity Framework 实体框架4.3.1 - &gt; 5.0异常:“EntityType没有定义键。 定义此EntityType的键。“ - Entity Framework 4.3.1 -> 5.0 Exception: “EntityType has no key defined. Define the key for this EntityType.” EntityType没有定义键。 定义此EntityType的键 - EntityType has no key defined. Define the key for this EntityType EntityType X没有定义键。 定义此EntityType的键 - EntityType X has no key defined. Define the key for this EntityType EntityType“帖子”没有定义键。 定义此EntityType的键 - EntityType 'posts' has no key defined. Define the key for this EntityType EntityType&#39;MyProfile&#39;没有定义键。 定义此EntityType的键 - EntityType 'MyProfile' has no key defined. Define the key for this EntityType EntityType &#39;Worker&#39; 没有定义键。 定义此 EntityType 的键 - EntityType 'Worker' has no key defined. Define the key for this EntityType 模型验证错误:EntityType 未定义键。 定义此 EntityType 的键 - Model validation error: EntityType has no key defined. Define the key for this EntityType EF 5.0 Fluent映射:“ EntityType&#39;{ClassMap}&#39;”未定义键。 定义此EntityType的密钥。” - EF 5.0 Fluent mapping: “EntityType '{ClassMap}' has no key defined. Define the key for this EntityType.” 数据库第一个EntityType&#39;表&#39;没有定义键。 定义此EntityType的键 - Database First EntityType 'table' has no key defined. Define the key for this EntityType
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM