Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Entity framework core : particular many to many relation

$
0
0

Hi,

I have the following Model class :

public class Terminal
    {
        public int id { get; set; }

        public string serialNumber { get; set; }
        public string model { get; set; }

        public virtual ICollection<ContainerTerminal> containerTerminals { get; set; }
    }

    public class Container
    {
        public int id { get; set; }

        [Required]
        public string code { get; set; }
        public string description { get; set; }
        public virtual ICollection<ContainerTerminal> containerTerminals { get; set; }
    }

    public class ContainerTerminal
    {
        public int id { get; set; }

        public int Containerid { get; set; }
        public virtual Container container { get; set; }

        public int Terminalid { get; set; }
        public virtual Terminal terminal { get; set; }

        public virtual ICollection<ModuleContainerTerminal> moduleContainerTerminals { get; set; }
    }

I did the mapping succesfully with :

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // removed for brevity ...


            builder.Entity<ContainerTerminal>()
                .HasKey(t => new { t.Terminalid, t.Containerid });

            builder.Entity<ContainerTerminal>()
            .HasOne(us => us.terminal)
            .WithMany(u => u.containerTerminals)
            .HasForeignKey(us => us.Terminalid);

            builder.Entity<ContainerTerminal>()
            .HasOne(us => us.container)
            .WithMany(u => u.containerTerminals)
            .HasForeignKey(us => us.Containerid);
        }

Now I want to link a new Module Table to containerTerminal with a many to many relation, here is what I did :

    public class Module
    {
        public int id { get; set; }

        [Required]
        public string name { get; set; }

        [Required]
        public string code { get; set; }

        public string description { get; set; }

        public virtual ICollection<ModuleContainerTerminal> moduleContainerTerminals { get; set; }
    }


    public class ModuleContainerTerminal
    {
        public int id { get; set; }

        public int Moduleid { get; set; }
        public virtual Module module { get; set; }

        public int ContainerTerminalid { get; set; }
        public virtual ContainerTerminal containerTerminal { get; set; }

        public DateTime dateStart { get; set; }
        public DateTime dateEnd { get; set; }

    }




with the following fluent api :

            builder.Entity<ModuleContainerTerminal>()
                .HasKey(t => new { t.ContainerTerminalid, t.Moduleid });

            builder.Entity<ModuleContainerTerminal>()
            .HasOne(us => us.containerTerminal)
            .WithMany(u => u.moduleContainerTerminals)
            .HasForeignKey(us => us.ContainerTerminalid);


            builder.Entity<ModuleContainerTerminal>()
            .HasOne(us => us.module)
            .WithMany(u => u.moduleContainerTerminals)
            .HasForeignKey(us => us.Moduleid);

I got the following error :

The relationship from 'ModuleContainerTerminal.containerTerminal' to 'ContainerTerminal.moduleContainerTerminals' with foreign key properties {'ContainerTerminalid' : int} cannot target the primary key {'Terminalid' : int, 'Containerid' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.

As I understood the composite key from ContainerTerminal (ContainerId,Terminalid) can't be linked with FK ContainerTerminalid from ModuleContainerTerminal. It's obviously normal (we can't put a composite key within an int). 

That's why I put an id key in ContainerTerminal in roder to use as the foreign key to use in ModuleContainerTerminal (ContainerTerminalid) butwith no effect at all.

Can someone help me with this, and tell me how to solve it ?

Many thanks


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>