JonPSmith / EfCore.GenericServices

A library to help you quickly code CRUD accesses for a web/mobile/desktop application using EF Core.
https://www.thereformedprogrammer.net/genericservices-a-library-to-provide-crud-front-end-services-from-a-ef-core-database/
MIT License
601 stars 94 forks source link

SetupEntitiesDirect fails if one uses records in the entities #60

Closed Akku-21 closed 2 years ago

Akku-21 commented 2 years ago
public class entity:
{
    [Key]
   public long Id { get; set; }
   public Reference Object { get; set; }
}
[Owned]
    public record Reference {

        public Reference(string type, Guid id)
        {
            Id = id;
            Type = type;
        }

        [StringLength(50)]  
        public string Type { get; set; }

        public Guid Id { get; set; }
    }

will fail with The class Reference was not found in the TestCtx DbContext. The class must be either be an entity class derived from the GenericServiceDto/Async class.

JonPSmith commented 2 years ago

hi @Akku-21,

The definition of your entity and Reference are fine, so it must be the way you are accessing them using GenericServices.

The error you list suggests you have a DTO linked to the Reference class, e.g class SomeClass LinkToEntity<Reference> { ..., but you don't need a link an Owned Type as when your entity is loaded if your DTO has a public Reference Object { get; set; } then that parameter will automatically filled in by EF Core.

If there is still a problem then please show your DTOs and the GenericServices code that creates an exception.

Akku-21 commented 2 years ago

First of all, thank you for the quick answer. I found out it has to do with .net 6 . I rolled back to .net5 and everything works again.

using (var context = new Context(new DbContextOptions<Context>()))
            {
                context.Database.EnsureCreated();
                var utData = context.SetupEntitiesDirect();

                var service = new CrudServices(context, utData.ConfigAndMapper);

where the context only has public DbSet<entity> Entities{ get; set; }

I have no dto linked to the Reference record.

JonPSmith commented 2 years ago

Are you saying that net6 isn't working? Have you updated to version 5.1.0 of the GenericServices library?

Akku-21 commented 2 years ago

yes that is what I am saying. I made a small app from scratch to test it. here are the results:

    public class entity
    {
        [Key]
        public long Id { get; set; }
        public Reference Object { get; set; }
        public Reference Object2 { get; set; }
    }

    [Owned]
    public class Reference
    {
        [StringLength(50)]
        public string Type { get; set; }
    } 
public class Context:DbContext
    {
        public Context(DbContextOptions<Context> options)
       : base(options)
        {
        }

        public DbSet<entity> Entities { get; set; }
    }
        public void Test1()
        {
            var options = SqliteInMemory.CreateOptions<Context>();
            using (var context = new Context(options))
            {
                context.Database.EnsureCreated();
                var utData = context.SetupEntitiesDirect();

                var service = new CrudServices(context, utData.ConfigAndMapper);
            }
        }

this will fail. however if you were to use just one field in the entity(remove prop object2) it will work.

thanks Jan

JonPSmith commented 2 years ago

Interesting. I have reproduced this error in this test. I will work on it, but I have other things so might be some days.

JonPSmith commented 2 years ago

Hi @Akku-21,

That was an interesting problem caused to the IEntityType for an Owned Type class changed in EF Core 6. I'm now using the IsOwned type which came out in EF Core 5.