Entity Framework 8 mapping a komplex namespace of Types (non-interface reference type Error)

JonasGraubner-8950 0 Zuverlässigkeitspunkte
2024-02-19T15:49:25.9966667+00:00

Hello,   I have the following namespace of types I want to map in EF.: ClassDiagram1 I know so far that I can't map the Interfaces directly. But how can I map the relations to EF?   I tried the following: (limited subset):

public DbSet<AasCore.Aas3_0.Key> KeyDbSets { get; set; }
public DbSet<AasCore.Aas3_0.Reference> ReferenceDbSets { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    //modelBuilder.Entity<Extension>().Property(e => e.SemanticId).HasConversion<Reference>(v => (Reference)v, v => (IReference)v);
    //.HasConversion(v => (Reference)v, v => (IReference)v);
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<Key>().HasKey(b => b.Value);
    modelBuilder.Entity<Reference>().HasMany(r => r.Keys);
    modelBuilder.Entity<Reference>().HasOne(r => (Reference)r.ReferredSemanticId);
}
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
    configurationBuilder
        .Properties<List<IReference>>()
        .HaveConversion<ReferenceInterfaceListConverter>();
    configurationBuilder
        .Properties<List<IKey>>()
        .HaveConversion<KeyInterfaceListConverter>();
}
public class ReferenceInterfaceListConverter : ValueConverter<List<IReference>, List<Reference>>
{
    public ReferenceInterfaceListConverter()
        : base(
            v => v.Cast<Reference>().ToList(),
            v => v.Cast<IReference>().ToList())
    {
    }
}
public class KeyInterfaceListConverter : ValueConverter<List<IKey>, List<Key>>
{
    public KeyInterfaceListConverter()
        : base(
            v => v.Cast<Key>().ToList(),
            v => v.Cast<IKey>().ToList())
    {
    }
}    

  definitions for Key Class & Interface:

public interface IKey : IClass
{
    public KeyTypes Type { get; set; }
    public string Value { get; set; }
}

public class Key : IKey
{
    public KeyTypes Type { get; set; }
    public string Value { get; set; }
    public IEnumerable<IClass> DescendOnce()
    {
        // No descendable properties
        yield break;
    }
    public IEnumerable<IClass> Descend()
    {
        // No descendable properties
        yield break;
    }
    public void Accept(Visitation.IVisitor visitor)
    {
        visitor.VisitKey(this);
    }

    public void Accept<TContext>(
        Visitation.IVisitorWithContext<TContext> visitor,
        TContext context)
    {
        visitor.VisitKey(this, context);
    }

    public T Transform<T>(Visitation.ITransformer<T> transformer)
    {
        return transformer.TransformKey(this);
    }

    public T Transform<TContext, T>(
        Visitation.ITransformerWithContext<TContext, T> transformer,
        TContext context)
    {
        return transformer.TransformKey(this, context);
    }

    public Key(
        KeyTypes type,
        string value)
    {
        Type = type;
        Value = value;
    }
}

  Definition for IReference:

public class Reference : IReference
{
    public ReferenceTypes Type { get; set; }
    public IReference? ReferredSemanticId { get; set; }

    public List<IKey> Keys { get; set; }

    public IEnumerable<IClass> DescendOnce()
    {
        if (ReferredSemanticId != null)
        {
            yield return ReferredSemanticId;
        }

        foreach (var anItem in Keys)
        {
            yield return anItem;
        }
    }

    public IEnumerable<IClass> Descend()
    {
        if (ReferredSemanticId != null)
        {
            yield return ReferredSemanticId;

            // Recurse
            foreach (var anItem in ReferredSemanticId.Descend())
            {
                yield return anItem;
            }
        }

        foreach (var anItem in Keys)
        {
            yield return anItem;

            // Recurse
            foreach (var anotherItem in anItem.Descend())
            {
                yield return anotherItem;
            }
        }
    }

    public void Accept(Visitation.IVisitor visitor)
    {
        visitor.VisitReference(this);
    }

    public void Accept<TContext>(
        Visitation.IVisitorWithContext<TContext> visitor,
        TContext context)
    {
        visitor.VisitReference(this, context);
    }

    public T Transform<T>(Visitation.ITransformer<T> transformer)
    {
        return transformer.TransformReference(this);
    }

    public T Transform<TContext, T>(
        Visitation.ITransformerWithContext<TContext, T> transformer,
        TContext context)
    {
        return transformer.TransformReference(this, context);
    }

    public Reference(
        ReferenceTypes type,
        List<IKey> keys,
        IReference? referredSemanticId = null)
    {
        Type = type;
        Keys = keys;
        ReferredSemanticId = referredSemanticId;
    }
}

The Result is the following error: Unhandled exception: System.AggregateException: One or ``more`` errors occurred. ``(``The specified ``type`` ``'AasCore.Aas3_0.IKey'`` must be a non-interface reference ``type`` to be used as an entity type.``) How can I map theses Classes and Interfaces correctly. Added Problem is that I can't modify the Definition of the Classes/ Interfaces.   Best regards

.NET
.NET
Microsoft-Technologien, die auf dem .NET-Softwareframework basieren.
29 Fragen
ASP.NET
ASP.NET
Eine Zusammenstellung von Technologien in Microsoft .NET Framework zum Erstellen von Webanwendungen und XML-Webdiensten.
2 Fragen
{count} Stimmen

Ihre Antwort

Fragesteller*innen können Antworten als akzeptierte Antworten markiert werden, wodurch Benutzer*innen wissen, dass diese Antwort das Problem gelöst hat.