question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error in SaveChanges() when EntityTypeMappings has multiple Fragments

See original GitHub issue

Hi, We are having an issue related to #273 We have an inheritance of four levels being RootClass and MiddleClass abstract classes. The problem occurs when we try to add an instance of LeafTwoClass.

DataBase Model

    public class BaseEntity
    {
        public int Id { get; set; }

        //more properties
    }
    public abstract class RootClass:BaseEntity
    {
        public string PropertyOne { get; set; }
        public string PropertyTwo { get; set; }
    }
    public abstract class MiddleClass:RootClass
    {
        public string PropertyThree { get; set; }
    }
    public class LeafOneClass:RootClass
    {
        public string PropertyFour { get; set; }
    }
    public class LeafTwoClass:MiddleClass
    {
        public string PropertyFive { get; set; }
    }

Fluent API Mapping

    public class BaseEntityMap<T> : EntityTypeConfiguration<T>        where T : BaseEntity
    {
        public BaseEntityMap()
        {
            HasKey(t => t.Id);
        }
    }
    public class RootClassMap:BaseEntityMap<RootClass>
    {
        public RootClassMap()
        {
            ToTable("RootClasses");
        }
    }
    public class LeafOneClassMap:BaseEntityMap<LeafOneClass>
    {
        public LeafOneClassMap()
        {
            ToTable("LeafOneClasses");
        }
    }
    public class LeafTwoClassMap:BaseEntityMap<LeafTwoClass>
    {
        public LeafTwoClassMap()
        {
            ToTable("LeafTwoClasses");
        }
    }

DbContext

    public class DataBaseContext:DbContext
    {
        private static DbContextHelper _helper = new DbContextHelper();
        private readonly IAuditDbContext _auditContext;

        public DataBaseContext() : base("DataBaseAuditTests")
        {
            _auditContext = new DefaultAuditContext(this);
            _helper.SetConfig(_auditContext);

            Audit.EntityFramework.Configuration.Setup().ForContext<DataBaseContext>(config =>
            {
                config.IncludeEntityObjects();
                config.IncludeIndependantAssociations();
            }).UseOptOut();
        }

        public override int SaveChanges()
        {
            return _helper.SaveChanges(_auditContext, () => base.SaveChanges());
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Configurations.Add(new RootClassMap());
            modelBuilder.Configurations.Add(new LeafOneClassMap());
            modelBuilder.Configurations.Add(new LeafTwoClassMap());
        }
    }

Exception

Sequence contains more than one element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at EntityKeyHelper.GetMappingFragment(Type type, DbContext context)
   at EntityKeyHelper.GetTableName(Type type, DbContext context)
   at Audit.EntityFramework.DbContextHelper.GetEntityName(DbContext dbContext, Object entity)
   at Audit.EntityFramework.DbContextHelper.CreateAuditEvent(IAuditDbContext context)
   at Audit.EntityFramework.DbContextHelper.SaveChanges(IAuditDbContext context, Func`1 baseSaveChanges)
  ...

To reproduce the issue we attach a project sample https://github.com/lbouza/AuditTest/tree/master

We are using the next libraries: Audit.Net 17.0.4 Audit.EntityFramework 17.0.4 EntityFramework 6.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lbouzacommented, Jun 10, 2021

The feature is working as expected. Thanks for your help and thanks for the library.

0reactions
thepirat000commented, Jun 5, 2021

This was fixed on version 17.0.6.

All tests are passing, please upgrade your references and re-test

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF 6 SaveChanges with multiple references to same ...
Set one of the Freinds to the same as the other Friend reference, e.g. Best = Oldest; SaveChanges(). Original Answer. My guess is...
Read more >
Error on SaveChanges() related to #132 · Issue #273
It then tries to get Column name for the Base Entity type (Mapping). When getting inside GetMappingFragment with the base type, the mapping....
Read more >
DbContext.SaveChanges Method
This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database.
Read more >
8 Entity Framework Gotchas
I have been working with EF for over two years and spent a good part of ... You can map them directly to...
Read more >
Error Problem in mapping fragments in Entity Framework(edmx)
This tutorial demonstrates how we can fix error Problem in mapping fragments in Entity Framework in asp.net project. Entity Framework (EDMX) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found