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 on context.SaveChanges() related to #55

See original GitHub issue

Hi!

I have the same issue as described on issue #55 . I still haven’t being able to actually tackle it, but I got a lot more details. My model is 3 layered (to avoid cluttering I’m removing code that is too obvious):

0st level:

public abstract class ValueObject<T> : IEntity, IValueObject<T> where T : IEntity
{
	public abstract int Id { get; set; }

	public static bool operator !=(ValueObject<T> @this, ValueObject<T> that) =>
			!(@this == that);

	public static bool operator ==(ValueObject<T> @this, ValueObject<T> that)
	{
		//...
	}

	public virtual int CompareTo(object other)
	{
		var that = other as ValueObject<T>;

		if (that == null)
			throw new ArgumentException("Impossible to compare different types");

		return this.Id.CompareTo(that.Id);
	}

	public abstract override bool Equals(object obj);

	public abstract bool Equals(T other);

	public override int GetHashCode() => this.Id.GetHashCode();

	public abstract override string ToString();
}

Generally the models inherit directly from it. But I do have a Person > Producer case wich is raising the problem:

1st level:

 public enum TipoPessoa
    {
        F,//isica
        J//uridica
    }

    public abstract class Person : ValueObject<Person>
    {
        public override int Id { get; set; }

        public TipoPessoa TipoPessoa { get; set; }

        public string PersonName { get; set; }

        public Person()
        {
            this.PersonName = string.Empty;
        }

        public override bool Equals(object obj)
        {
		//...
        }

        public override bool Equals(Person other) =>
            this.Id == other.Id &&
            this.PersonName == other.PersonName;

        public override string ToString() =>
                $@"Person : [ NomePessoa: {this.PersonName} ]";
    }

2nd level:

    public enum Situacao
    {
        N, //N/A
        A, //Ativo
        I, //Inativo
        C, //em Cadastro
        S  //Suspenso
    }


 public class Producer : Person
    {
        public Situacao Situacao { get; set; }

        public override bool Equals(object obj)
        {
		//...
        }

        public bool Equals(Producer other) =>
            this.Id == other.Id &&
            base.Equals(other);

        public override string ToString() =>
                $@"Producer : [ Situação: {this.Situacao.ToString()}, {base.ToString()} ]";
    }

So I followed all the stack error (wich is quite similar to #55):

System.AggregateException: Um ou mais erros. ---> System.InvalidOperationException: A sequência contém mais de um elemento
   em System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   em EntityKeyHelper.GetMappingFragment(Type type, DbContext context)
   em EntityKeyHelper.GetColumnName(Type type, String propertyName, DbContext context)
   em EntityKeyHelper.GetColumnName(Type type, String propertyName, DbContext context)
   em Audit.EntityFramework.DbContextHelper.GetColumnValues(IAuditDbContext context, DbEntityEntry entry)
   em Audit.EntityFramework.DbContextHelper.CreateAuditEvent(IAuditDbContext context)
   em Audit.EntityFramework.DbContextHelper.SaveChanges(IAuditDbContext context, Func`1 baseSaveChanges)
   em Audit.EntityFramework.AuditDbContext.SaveChanges()

and copied the method GetMappingFragment into my project (took it from here) and made this tiny test:

            try
            {
                GetMappingFragment(typeof(Person), new ApplicationOrmConfiguration(isMigration: true));
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                if (ex.InnerException != null)
                    logger.Error(ex.InnerException);
            }

What happens it that on line 110 (of EntityKeyHelper) I have two entries of EntityTypeMappings. (I’m behind a corporate firewall so I’m pasting the Quickwatch table in text form):

| Name | Value | Type – | – | – | – ◢ | mapping.EntityTypeMappings | Count = 2 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Mapping.EntityTypeMapping>   | ◢ [0] | {System.Data.Entity.Core.Mapping.EntityTypeMapping} | System.Data.Entity.Core.Mapping.EntityTypeMapping   | ▶ Annotations | Count = 0 | System.Collections.Generic.IList<System.Data.Entity.Core.Metadata.Edm.MetadataProperty> {System.Collections.Generic.List<System.Data.Entity.Core.Metadata.Edm.MetadataProperty>}   | ▶ EntitySetMapping | {System.Data.Entity.Core.Mapping.EntitySetMapping} | System.Data.Entity.Core.Mapping.EntitySetMapping   | ▶ EntityType | {Icatu.SisPac.Orm.Produtor} | System.Data.Entity.Core.Metadata.Edm.EntityType   | ▶ EntityTypes | Count = 1 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntityTypeBase>   | ▶ Fragments | Count = 1 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Mapping.MappingFragment>   | IsHierarchyMapping | false | bool   | ▶ IsOfEntityTypes | Count = 0 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntityTypeBase>   | ▶ IsOfTypes | Count = 0 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntityTypeBase>   | IsReadOnly | true | bool   | ▶ MappingFragments | Count = 1 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Mapping.MappingFragment>   | ▶ SetMapping | {System.Data.Entity.Core.Mapping.EntitySetMapping} | System.Data.Entity.Core.Mapping.EntitySetBaseMapping {System.Data.Entity.Core.Mapping.EntitySetMapping}   | ▶ Types | Count = 1 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntityTypeBase>   | ▶ _annotations | Count = 0 | System.Collections.Generic.List<System.Data.Entity.Core.Metadata.Edm.MetadataProperty>   | ▶ _entitySetMapping | {System.Data.Entity.Core.Mapping.EntitySetMapping} | System.Data.Entity.Core.Mapping.EntitySetMapping   | ▶ _entityType | {Icatu.SisPac.Orm.Produtor} | System.Data.Entity.Core.Metadata.Edm.EntityType   | ▶ _fragments | Count = 1 | System.Collections.Generic.List<System.Data.Entity.Core.Mapping.MappingFragment>   | _readOnly | true | bool   | ▶ m_entityTypes | Count = 1 | System.Collections.Generic.Dictionary<string, System.Data.Entity.Core.Metadata.Edm.EntityType>   | ▶ m_isOfEntityTypes | Count = 0 | System.Collections.Generic.Dictionary<string, System.Data.Entity.Core.Metadata.Edm.EntityType>   | ◢ [1] | {System.Data.Entity.Core.Mapping.EntityTypeMapping} | System.Data.Entity.Core.Mapping.EntityTypeMapping   | ▶ Annotations | Count = 0 | System.Collections.Generic.IList<System.Data.Entity.Core.Metadata.Edm.MetadataProperty> {System.Collections.Generic.List<System.Data.Entity.Core.Metadata.Edm.MetadataProperty>}   | ▶ EntitySetMapping | {System.Data.Entity.Core.Mapping.EntitySetMapping} | System.Data.Entity.Core.Mapping.EntitySetMapping   | ▶ EntityType | null | System.Data.Entity.Core.Metadata.Edm.EntityType   | ▶ EntityTypes | Count = 0 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntityTypeBase>   | ▶ Fragments | Count = 1 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Mapping.MappingFragment>   | IsHierarchyMapping | true | bool   | ▶ IsOfEntityTypes | Count = 1 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntityTypeBase>   | ▶ IsOfTypes | Count = 1 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntityTypeBase>   | IsReadOnly | true | bool   | ▶ MappingFragments | Count = 1 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Mapping.MappingFragment>   | ▶ SetMapping | {System.Data.Entity.Core.Mapping.EntitySetMapping} | System.Data.Entity.Core.Mapping.EntitySetBaseMapping {System.Data.Entity.Core.Mapping.EntitySetMapping}   | ▶ Types | Count = 0 | System.Collections.ObjectModel.ReadOnlyCollection<System.Data.Entity.Core.Metadata.Edm.EntityTypeBase>   | ▶ _annotations | Count = 0 | System.Collections.Generic.List<System.Data.Entity.Core.Metadata.Edm.MetadataProperty>   | ▶ _entitySetMapping | {System.Data.Entity.Core.Mapping.EntitySetMapping} | System.Data.Entity.Core.Mapping.EntitySetMapping   | ▶ _entityType | null | System.Data.Entity.Core.Metadata.Edm.EntityType   | ▶ _fragments | Count = 1 | System.Collections.Generic.List<System.Data.Entity.Core.Mapping.MappingFragment>   | _readOnly | true | bool   | ▶ m_entityTypes | Count = 0 | System.Collections.Generic.Dictionary<string, System.Data.Entity.Core.Metadata.Edm.EntityType>   | ▶ m_isOfEntityTypes | Count = 1 | System.Collections.Generic.Dictionary<string, System.Data.Entity.Core.Metadata.Edm.EntityType>

Am I doing something terribly wrong?

Edit 1: I wrote everything kind in a hurry and don’t think I made myself completely clear. But the error is raised when using Producer (I know you could have guessed it, but nonetheless), on my test I just used the base class (Person) to go directly to what was happening.

Remark 1: I have another use case that has just manifested itself it’s a Product > BusinessLineProduct relationship which is expected to have many more (10+) inheritance cases. So a more general sulution would be awsome! I’m working on trying to “avoid” this case, but I’m guessing I won’t be able to since I can’t figure out why there’s a “Null” EntityTypeMappings =/

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Jun 8, 2018

Give me a moment. I’ll make a new solution to provide you the exactly same error ok?

I can’t just post the code here (unfortunately).

0reactions
thepirat000commented, Jul 1, 2018

Closing this for inactivity.

Feel free to re-open if you’re still having issues and can provide a way to reproduce it

Read more comments on GitHub >

github_iconTop Results From Across the Web

dbContext.SaveChanges() not saving and not outputting ...
When you create your new city object you do this: var newCity = new City() { Postcode = innUser.Postcode, Cityname = innUser.city.
Read more >
Error on context.SaveChanges() #55 - thepirat000/Audit.NET
Hello, I'm stuck on this error trying to save my entity with the library: em System.Linq.Enumerable. ... Error on context.SaveChanges() #55.
Read more >
Where to put SaveChanges in unit of work?
Even though the DAL code still calls SaveChanges , the context ignores these calls because it's aware that a transaction has been registered...
Read more >
DbContext.SaveChanges Method
This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled...
Read more >
context.SaveChanges() not persisting data in database
Hello, Im working on a MVC app. When I call context.SaveChanges to update a specific records. The update is not registered in the...
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