4

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";
        }
    }
4
  • 3
    How do you save an Exception in a database, or a StopWatch? EF thinks these are tables you've created in the database and is looking to reference them and grab some foreign keys to put in your Tasks table. EF just likes dealing with data, it is confused. You can start by making those members private, or better yet separating out the data you want to persist from the class that creates it. Commented Jun 28, 2013 at 1:48
  • What does your task database table look like? Could you post the table definition? Commented Jun 28, 2013 at 2:39
  • Thanks dbugger, that was it!!!! Commented Jun 28, 2013 at 2:42
  • First create primary key then it will resolve Commented Jun 28, 2013 at 5:13

1 Answer 1

1

Had to use [NotMapped] attribute on some properties.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.