Share via

Question about Entity framework optimization

IKINGSHADOW 136 Reputation points
2022-04-20T21:47:05.443+00:00

Hello everyone ,
so I have question about entity framework ,
I want to create a music player application in WPF and i want to use entity framework , so in my application i will get folders from users and check the folders for music files and then add thier address and some of information like music name , artist name , album name , ETC into the Database and for this purpose i decided to seperate the Artists and albums and address into diffrent tables , it means i have a main tables that i will store the name , lengh and date of music plus the id of music artists , id of album and id of address , so this way instead of the name of artist or the name of album i will just save a id into my main table , so for this purpose i decided to check this theory on action and then i wrote this code but i think my code is not well enough for this purpose , could you help me to improve my code ??

    NicePlayerDBEntities myentity = new NicePlayerDBEntities();
    public bool addmusicintodb(string name,int lengh,DateTime dateofcreation,string artistname,string albumname,string address)
    {
        MusicsList_TB MyMusic = new MusicsList_TB();
        Paths_TB musicaddress = new Paths_TB();
        Artist_TB MusicArtist = new Artist_TB();
        Albums_TB MusicAlbum = new Albums_TB();

        int addressID;
        int artistID;
        int albumID;

        string Name = name;
        int Lengh = lengh;
        DateTime Dateofcreation = dateofcreation;

        string Artistname = artistname;
        string Albumname = albumname;

        string Address = address;

        // adding address
        musicaddress.Address = Address;
        MusicArtist.ArtitstName = Artistname;
        MusicAlbum.AlbumName = Albumname;


        myentity.Paths_TB.Add(musicaddress);
        myentity.Artist_TB.Add(MusicArtist);
        myentity.Albums_TB.Add(MusicAlbum);
        myentity.SaveChanges();

        // retrive the address id
        addressID = (from db in myentity.Paths_TB where db.Address == Address select db.AddressID).FirstOrDefault();
        artistID = (from db in myentity.Artist_TB where db.ArtitstName == Artistname select db.ArtistID).FirstOrDefault();
        albumID = (from db in myentity.Albums_TB where db.AlbumName == Albumname select db.AlbumID).FirstOrDefault();

        //addressID = myentity.Paths_TB.Where(x => x.Address == Address).Select(x => x.AddressID).FirstOrDefault();

        MyMusic.MusicName = Name;
        MyMusic.Lengh = Lengh;
        MyMusic.DateofCreation = Dateofcreation;
        MyMusic.DateofAdd = DateTime.Now;
        MyMusic.ArtistID = artistID;
        MyMusic.AlbumID = albumID;
        MyMusic.AddressID = addressID;
        myentity.MusicsList_TB.Add(MyMusic);
        myentity.SaveChanges();


        return true;
    }
Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-04-20T23:02:39.327+00:00

    Unsure of the database schema but it would be prudent to first create the database and tables, populate the tables with mocked data and see if you can get what you want. High level, from seeing your tables there should be joins e.g. artist can have one or more alums and albums can have one or more songs. You should for instance from song get to album from album to artist.

    Next steps after verifying the above schema is good to go, scaffold the database by command line or a tool such as EF Power Tools.

    So don't work from code first, think out a good database schema and test it before going to code.

    Also, unsure why you have a param to the method like string name than later assign it to another local variable, not needed.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.