Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyNotFoundException in DatabaseFirst #108

Open
pointlevel opened this issue May 25, 2016 · 7 comments
Open

KeyNotFoundException in DatabaseFirst #108

pointlevel opened this issue May 25, 2016 · 7 comments

Comments

@pointlevel
Copy link

Hello!

I get a KeyNotFoundException "Key not found for TestProject.Setting" when trying to use the tracker.

Stacktrace:
vid TrackerEnabledDbContext.Common.Auditors.LogAuditor.GetPrimaryKeyValuesOf(DbEntityEntry dbEntry, List`1 properties)
vid TrackerEnabledDbContext.Common.Auditors.LogAuditor.CreateLogRecord(Object userName, EventType eventType, ITrackerContext context)
vid TrackerEnabledDbContext.Common.CoreTracker.AuditChanges(Object userName)
vid TrackerEnabledDbContext.TrackerContext.SaveChanges(Object userName)
vid TrackerEnabledDbContext.TrackerContext.SaveChanges()

My table has a primary key, and i have tried also with annotations, no difference. Tried to track other tables but with same result. Im using EF 6. Any ideas?

@bilal-fazlani
Copy link
Owner

Show me your dbcontext code.

And its happening while saving a particular entity or all entities ?

@pointlevel
Copy link
Author

pointlevel commented May 25, 2016

'------------------------------------------------------------------------------
' <auto-generated>
'    This code was generated from a template.
'
'    Manual changes to this file may cause unexpected behavior in your application.
'    Manual changes to this file will be overwritten if the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Imports TrackerEnabledDbContext

Partial Public Class MyEntities
    Inherits TrackerContext

    Public Sub New()
        MyBase.New("name=MyEntities")
    End Sub

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        Throw New UnintentionalCodeFirstException()
    End Sub

    Public Property Items() As DbSet(Of Item)
    Public Property Users() As DbSet(Of User)
    Public Property Logs() As DbSet(Of Log)
    Public Property Settings() As DbSet(Of Setting)

End Class

The exception occurs only when there is changes in the entity, but the same exception for all entities, yes.

@bilal-fazlani
Copy link
Owner

This looks pretty normal... except for the fact that its auto generated.
Which is not usually the case in code first.

Are you using code first ?

Can u show me the code for above entities ?

On Wed 25 May, 2016 9:49 pm pointlevel, notifications@github.com wrote:

'------------------------------------------------------------------------------
'
' This code was generated from a template.
'
' Manual changes to this file may cause unexpected behavior in your application.
' Manual changes to this file will be overwritten if the code is regenerated.
'
'------------------------------------------------------------------------------

Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Imports TrackerEnabledDbContext

Partial Public Class MyEntities
Inherits TrackerContext

Public Sub New()
    MyBase.New("name=MyEntities")
End Sub

Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
    Throw New UnintentionalCodeFirstException()
End Sub

Public Property Items() As DbSet(Of Item)
Public Property Users() As DbSet(Of User)
Public Property Logs() As DbSet(Of Log)
Public Property Settings() As DbSet(Of Setting)

End Class

The exception occurs only when there is changes in the entity, but the
same exception for all entities, yes.


You are receiving this because you commented.

Reply to this email directly or view it on GitHub
#108 (comment)

@pointlevel
Copy link
Author

Hello!

Im using DataBase first and the models are generated automatic from the DB. Is that a problem?

@bilal-fazlani
Copy link
Owner

This library works with code first only.

@bilal-fazlani
Copy link
Owner

I did some debugging to find out why its crashing with that exception and I found that its a bug in EntityFramework.MappingApi library.

https://efmappingapi.codeplex.com/workitem/1825

I have raised a bug there.

@pointlevel
Copy link
Author

Great discover, hopefully they will fix it, or i have to change to Code First!

@bilal-fazlani bilal-fazlani changed the title KeyNotFoundException? KeyNotFoundException in DatabaseFirst May 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants