Error on context.SaveChanges() related to #55
See original GitHub issueHi!
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:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
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).
Closing this for inactivity.
Feel free to re-open if you’re still having issues and can provide a way to reproduce it