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;
}