edandersen / core-admin

Fully automatic admin site CRUD UI generator for ASP.NET Core and .NET 8
Other
599 stars 111 forks source link

MissingMethodException if the Entity only contains foreign key column #74

Closed neonleo closed 2 years ago

neonleo commented 2 years ago

I using the chinook sample DB to trying core admin, the (PlaylistTrack) table reference to (PlayList) and (Track) tables, and I have check both of this table is work fine on core admin, And the DB schema:

CREATE TABLE [PlaylistTrack]
(
    [PlaylistId] INTEGER  NOT NULL,
    [TrackId] INTEGER  NOT NULL,
    CONSTRAINT [PK_PlaylistTrack] PRIMARY KEY  ([PlaylistId], [TrackId]),
    FOREIGN KEY ([PlaylistId]) REFERENCES [Playlist] ([PlaylistId]) 
        ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([TrackId]) REFERENCES [Track] ([TrackId]) 
        ON DELETE NO ACTION ON UPDATE NO ACTION
)

The EntityClass:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace ApplicationCore.Entities
{
    [Table("PlaylistTrack")]
    public class PlayListTrack
    {
        private long _PlayListId;
        [Display(AutoGenerateField = false)] // prevent showing on the grid
        public long PlayListId
        {
            get { return _PlayListId; }
            set { _PlayListId = value; }
        }

        private long _TrackId;
        [Display(AutoGenerateField = false)] // prevent showing on the grid
        public long TrackId
        {
            get { return _TrackId; }
            set { _TrackId = value; }
        }

        [ForeignKey("PlayListId")]
        public virtual PlayList? PlayList { get; set; }

        [ForeignKey("TrackId")]
        public virtual Track? Track { get; set; }

        public PlayListTrack(long playListId, long trackId)
        {
            this.PlayListId = playListId;
            this.TrackId = trackId;
        }
    }
}

The RepositoryContext class as well:

using Microsoft.EntityFrameworkCore;

namespace ApplicationCore.Entities
{
    public class RepositoryContext : DbContext
    {
        public RepositoryContext(DbContextOptions<RepositoryContext> options) : base(options)
        {
        }
        public DbSet<PlayList>? PlayLists { get; set; }
        public DbSet<PlayListTrack>? PlayListTracks { get; set; }
        public DbSet<Track>? Tracks { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<PlayListTrack>().HasKey(table => new {
                table.PlayListId,
                table.TrackId
            });
        }

    }
}

The PlaylistTrack entity display is fine, but missing edit button and if I press the Create button, an unhandled exception occurred: image

image

I guess because the primary key contains multiple columns and Core Admin don't handle this key type?

edandersen commented 2 years ago

PlaylistTrack entity needs a parameterless constructor.


From: Leo Cheung @.> Sent: Saturday, July 23, 2022 7:16:13 PM To: edandersen/core-admin @.> Cc: Subscribed @.***> Subject: [edandersen/core-admin] MissingMethodException if the Entity only contains foreign key column (Issue #74)

I using the chinook sample DBhttps://github.com/lerocha/chinook-database/tree/master/ChinookDatabase/DataSources to trying core admin, the (PlaylistTrack) table reference to (PlayList) and (Track) tables, and I have check both of this table is work fine on core admin, And the DB schema: CREATE TABLE [PlaylistTrack] ( [PlaylistId] INTEGER NOT NULL, [TrackId] INTEGER NOT NULL, CONSTRAINT [PK_PlaylistTrack] PRIMARY KEY ([PlaylistId], [TrackId]), FOREIGN KEY ([PlaylistId]) REFERENCES [Playlist] ([PlaylistId]) ON DELETE NO ACTION ON UPDATE NO ACTION, FOREIGN KEY ([TrackId]) REFERENCES [Track] ([TrackId]) ON DELETE NO ACTION ON UPDATE NO ACTION )

The EntityClass: `using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;

namespace ApplicationCore.Entities { [Table("PlaylistTrack")] public class PlayListTrack { private long _PlayListId; [Display(AutoGenerateField = false)] // prevent showing on the grid public long PlayListId { get { return _PlayListId; } set { _PlayListId = value; } }

    private long _TrackId;
    [Display(AutoGenerateField = false)] // prevent showing on the grid
    public long TrackId
    {
            get { return _TrackId; }
            set { _TrackId = value; }
    }

    [ForeignKey("PlayListId")]
    public virtual PlayList? PlayList { get; set; }

    [ForeignKey("TrackId")]
    public virtual Track? Track { get; set; }

    public PlayListTrack(long playListId, long trackId)
    {
            this.PlayListId = playListId;
            this.TrackId = trackId;
    }

}

} ` The RepositoryContext class as well:

`using Microsoft.EntityFrameworkCore;

namespace ApplicationCore.Entities { public class RepositoryContext : DbContext { public RepositoryContext(DbContextOptions options) : base(options) { } public DbSet? PlayLists { get; set; } public DbSet? PlayListTracks { get; set; } public DbSet? Tracks { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<PlayListTrack>().HasKey(table => new {
        table.PlayListId,
        table.TrackId
    });
}

}

} `

The PlaylistTrack entity display is fine, but missing edit button and if I press the Create button, an unhandled exception occurred: [image]https://user-images.githubusercontent.com/31003301/180617716-54a5c0e0-3bd3-4fe9-97a3-e0e0a8d9bbb6.png

[image]https://user-images.githubusercontent.com/31003301/180617703-71c6e332-6a86-4ffc-9e67-229e4499cb7b.png

— Reply to this email directly, view it on GitHubhttps://github.com/edandersen/core-admin/issues/74, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAEF6HEULOOMZS3MWX5TQPLVVQZG3ANCNFSM54OLJTPQ. You are receiving this because you are subscribed to this thread.Message ID: @.***>

neonleo commented 2 years ago

thanks you ,it is works now.