Entity Framework CodeFirst KeyNotFoundException in MemberDomainMap

前端 未结 3 1505
傲寒
傲寒 2021-01-06 16:55

Trying to run down an error in my EF datacontext implementation that is yielding a fairly cryptic error.

Test Name:  Nodes_can_be_saved
Test FullName:  MyP         


        
相关标签:
3条回答
  • 2021-01-06 17:22

    Well, after much searching through my code and rebuilding from scratch I found that the problem was actually that I had a derived class of Node that had a Uri as a property, which obviously failed mapping since it doesn't have a default constructor (and possibly other reasons). I solved it for now by simply changing the property to a String which I validate as a Uri internally, though I would have preferred a more elegant solution. I tried mapping Uri and even a custom subclass (w/ default constructor) of Uri to a complextype, but that didn't help.

    Still, the question above is answered.

    0 讨论(0)
  • 2021-01-06 17:36

    With the @Paul's answer I could finally figure out my problem.

    I am using EF with Inheritance TPT (Table per Type).

    The source code

    To make it easier I'll use the same classes discribed in this tutorial.

    public abstract class BillingDetail
    {
        public int BillingDetailId { get; set; }
        public string Owner { get; set; }
        public string Number { get; set; }
    }
    
    [Table("BankAccounts")]
    public class BankAccount : BillingDetail
    {
        public string BankName { get; set; }
        public string Swift { get; set; }
        public Agency Agency { get; set; } /* I added it */
    }
    
    public class InheritanceMappingContext : DbContext
    {
        public DbSet<BillingDetail> BillingDetails { get; set; }
    }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BankAccount>().ToTable("BankAccounts");
        modelBuilder.Entity<CreditCard>().ToTable("CreditCards");
    }
    

    The problem

    Note that I've added a new property called Agency inside the BankAccount. Given it is a complex type, if you do not map it you'll get this annoying error at runtime!

    Solution

    What I did was simply ignore this property Agency, but you can also map it to EF know what to do. Both will stop the error.

    The most weird thing is that even not mapping the derived entity (BankAccount) the problem occurs. It seems that EF somehow knows that you created the derivation. So, if you're trying to run EF without mapping some derivation you will probably get this error too.

    0 讨论(0)
  • 2021-01-06 17:37

    I have the same issue, unfortunately none of the solutions here in StackOverflow worked for me aside from the answers on other questions related to this issue.

    But I found my own fix and you can also check it in your part if you have the same issue as mine. What happen is that if some class inherits from the table that I am using on my DbSet:

    public DbSet<Employee> Employees { get; set; }
    

    On this case, if some other classes inherit from my POCO Employee, this triggers this particular error. I removed all inheritance from this class and this fixes the issue.

    Take note that this inheritance issue which triggers this same issue:

    The given key was not present in the dictionary.
    

    Only happens if the inheritance is on the same project. I tried to inherit the POCO on different project and it happens to be fine.

    0 讨论(0)
提交回复
热议问题