Giter VIP home page Giter VIP logo

Comments (12)

leonardoporro avatar leonardoporro commented on May 25, 2024 1

Hi @tarockx, thank you for the feedback! I'll be able to work on this next friday.

from detached-mapper.

tarockx avatar tarockx commented on May 25, 2024

Hello @leonardoporro , have you had any chance to look at this? Any progress?

from detached-mapper.

leonardoporro avatar leonardoporro commented on May 25, 2024

Hi @tarockx,
Yes, some progress... would you like to give it a try?
It's not completed, so for the moment, the discriminator needs to be set up in Detached too.
Please take a look here Line 68.
If it works, next step would be to create a Convention to copy the configuration automatically.
Sorry for the delay, I have very limited time to work on this.

from detached-mapper.

leonardoporro avatar leonardoporro commented on May 25, 2024

published 6.0.5, it doesn't need to be manually configured anymore.. (if it works).

from detached-mapper.

tarockx avatar tarockx commented on May 25, 2024

Thank you for your work, really appreciated, I will surely test it as soon as I have the time and provide feedback

from detached-mapper.

tarockx avatar tarockx commented on May 25, 2024

Hello Leonardo.
From my preliminary tests, I can confirm it seems to be working correctly! Thank you very much for your work and dedication 👍

from detached-mapper.

leonardoporro avatar leonardoporro commented on May 25, 2024

Awesome, still need to work on back references when using inheritance...
But will close this for now.
Thanks for the feedback and for the testing.
Stars / shares / help is always welcome!

from detached-mapper.

muellercornelius avatar muellercornelius commented on May 25, 2024

Hi Leonardo ;) thank you for your nice package. In my tests i can confirm that Detached finds the correct Discriminator but i get this error message:
2 is not a valid value for discriminator in entity My.Wonderful.Entity

my Discriminator is set up like this:

modelBuilder.Entity<RollenBase>()
                .HasDiscriminator(r => r.RollenTyp)
                .HasValue<KontaktRolle>(1)
                .HasValue<BauvorhabenKontaktRolle>(2)
                .HasValue<PersonalRolle>(3);

I tried it with strings as well already.

modelBuilder.Entity<RollenBase>()
                .HasDiscriminator(r => r.RollenTyp)
                .HasValue<KontaktRolle>(nameof(KontaktRolle))
                .HasValue<BauvorhabenKontaktRolle>(nameof(BauvorhabenKontaktRolle))
                .HasValue<PersonalRolle>(nameof(PersonalRolle));

But I got the same error message...

BauvorhabenKontaktRolle is not a valid value for discriminator in entity My.Wonderful.Entity

My classes are very simple:

public abstract class RollenBase
   {
       [Key]
       public int Id { get; set; }
       public string RollenTyp { get; set; }
       public string Name { get; set; }
   }
public class BauvorhabenKontaktRolle : RollenBase
    {
        public int RollenId { get; set; }

        //some props
    }

And my DTO has only three properties.

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

        public string Name { get; set; }

        public string RollenTyp  {get; set;}
    }

Did I configured something wrong?

Thank you for your help and work ;).

from detached-mapper.

leonardoporro avatar leonardoporro commented on May 25, 2024

This works for me...

using System.ComponentModel.DataAnnotations;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Detached.Mappers.EntityFramework;
using System.Diagnostics;

namespace Test
{

    public class Program
    {
        public static async Task Main(string[] args)
        {
            TestDbContext context = await TestDbContext.CreateAsync("inheritance_test");

            var r1 = context.Map<RollenBase>(new BauvorhabenKontaktRolleDTO
            {
                Id = 1,
                Name = "kontakt",
                RollenTyp = nameof(KontaktRolle)
            });

            var r2 = context.Map<RollenBase>(new BauvorhabenKontaktRolleDTO
            {
                Id = 2,
                Name = "bauvorhabenKontakt",
                RollenTyp = nameof(BauvorhabenKontaktRolle)
            });

            var r3 = context.Map<RollenBase>(new BauvorhabenKontaktRolleDTO
            {
                Id = 3,
                Name = "personalRolle",
                RollenTyp = nameof(PersonalRolle)
            });

            Debug.Assert(r1.GetType() == typeof(KontaktRolle));
            Debug.Assert(r2.GetType() == typeof(BauvorhabenKontaktRolle));
            Debug.Assert(r3.GetType() == typeof(PersonalRolle));

            Console.WriteLine("it works!");
        }
    }

    public abstract class RollenBase
    {
        [Key]
        public int Id { get; set; }

        public string RollenTyp { get; set; }

        public string Name { get; set; }
    }

    public class BauvorhabenKontaktRolle : RollenBase
    {
        public int RollenId { get; set; }

        //some props
    }

    public class KontaktRolle : RollenBase
    {
        public int RollenId { get; set; }

        //some props
    }
    public class PersonalRolle : RollenBase
    {
        public int RollenId { get; set; }

        //some props
    }

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

        public string Name { get; set; }

        public string RollenTyp { get; set; }
    }

    public class TestDbContext : DbContext
    {
        public TestDbContext(DbContextOptions<TestDbContext> options)
            : base(options)
        {
        }

        public DbSet<RollenBase> RollenBases { get; set; }

        protected override void OnModelCreating(ModelBuilder mb)
        {
            mb.Entity<RollenBase>()
                .HasDiscriminator(r => r.RollenTyp)
                .HasValue<KontaktRolle>(nameof(KontaktRolle))
                .HasValue<BauvorhabenKontaktRolle>(nameof(BauvorhabenKontaktRolle))
                .HasValue<PersonalRolle>(nameof(PersonalRolle));
        }

        public static async Task<DbContextOptions<TestDbContext>> CreateOptionsAsync([CallerMemberName] string dbName = null)
        {
            var connection = new SqliteConnection($"DataSource=file:{dbName}?mode=memory&cache=shared");

            await connection.OpenAsync();

            return new DbContextOptionsBuilder<TestDbContext>()
                    .UseSqlite(connection)
                    .UseDetached(cfg =>
                    {

                    })
                    .Options;
        }

        public static async Task<TestDbContext> CreateAsync([CallerMemberName] string dbName = null)
        {
            var context = new TestDbContext(await CreateOptionsAsync(dbName));
            await context.Database.EnsureCreatedAsync();
            return context;
        }
    }
}

from detached-mapper.

leonardoporro avatar leonardoporro commented on May 25, 2024

Added another fix in 6.06

from detached-mapper.

muellercornelius avatar muellercornelius commented on May 25, 2024

Hi Leonardo ;) thank you very much for your help.

After upgrading it works fine now.

from detached-mapper.

leonardoporro avatar leonardoporro commented on May 25, 2024

Inheritance works better in version 6.1.0. Also handling abstract types has been improved.

from detached-mapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.