Home > Blockchain >  Entity Framework cannot insert data
Entity Framework cannot insert data

Time:11-01

I am making this a school project and I am first time using the Entity Framework so don't be too harsh about it. These are my tables/classes.

public class Database : DbContext
{
    public Database() {} 

    public DbSet<Producer> Producers { get; set; } 
    public DbSet<Song> Songs { get; set; }
    public DbSet<ProducerSong> ProducerGlasbos { get; set; }
}

public class Song
{
    public int Id { get; set; }
    
    public string Title { get; set; }
    public int Length { get; set; }
    public int Views { get; set; }
}

public class Producer
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    
    public string name { get; set; }
    public string surname { get; set; }
    public int birth { get; set; }
}

public class ProducerSong
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    
    public Producer Producer { get; set; }
    public Song Songs { get; set; }
    
    public int year { get; set; }
}

When I try to inset new instance of a class into table/database in just crashes and throws error

Data.Glasbas.Add(new Glasba { length = 200, Naslov = "Bolecina", views= 313});

Error screenshot: Look at me

Thanks for helping.

CodePudding user response:

You have many to many relations and need to add some navigation properties. Try to change ProducerSong and migrate your db

 public class ProducerSong
    {

       public int  ProducerId { get; set; }
        
        public  int SongId { get; set; }
        
        public  Producer Producer { get; set; }
        
        public  Song Song { get; set; }
        
        public  int year { get; set; }

    }

CodePudding user response:

The problem was in constructors if I used constructors to create objects it just don't work odd but what can you do..

CodePudding user response:

public class Database : DbContext
{
    public Database() { }
    public DbSet<Producer> Producers { get; set; }
    public DbSet<Song> Songs { get; set; }
    public DbSet<ProducerSong> ProducerGlasbos { get; set; }

    public class ProducerSong
    {
        [System.ComponentModel.DataAnnotations.Key]
        public int ProducerSongID { get; set; }

        public Producer Producer { get; set; }
        public Song Songs { get; set; }
        public int year { get; set; }
    }

    public class Song
    {
        [System.ComponentModel.DataAnnotations.Key]
        public int Id { get; set; }

        public string Title { get; set; }
        public int lenght { get; set; }
        public int views { get; set; }

        //ForeignKey
        public int ProducerSongID { get; set; }
        public ProducerSong ProducerSong { get; set; }
    }

    public class Producer
    {
        [System.ComponentModel.DataAnnotations.Key]
        public int Id { get; set; }

        public string name { get; set; }
        public string surname { get; set; }
        public int birth { get; set; }

        //ForeignKey
        public int ProducerSongID { get; set; }
        public ProducerSong ProducerSong { get; set; }
    }
}
  • Related