开发者

ASP.net Entity-Framework (Basic) Questions

(ASP.net MVC 3.0 Razor, EF Code First)

My first question is: where do I store my data? in the Initializer or in the .sdf?

And my second question is this: I have a class for Songs, and one for Artists.

This is from my Initializer:

var Artists= new List<Artist>()
        {
            new Artist{ Name = "ArtistName", Birthday=DateTime.Parse("4-4-1988")}
        };
        Authors.ForEach(s => context.Authors.Add(s));
        context.SaveChanges();

        var Songs= new List<Song>()
        {
            new Song { Name="SongName", Genre = Genre.GenreTypes.Rock, Artist=Artists[0]} //<--
        };
        Books.ForEach(s => context.Books.Add(s));
        context.SaveChanges();

Where the green arrow is, is the problem. I'm trying to get the artist by the list-index above (where I loaded the artists into the database), but when开发者_StackOverflow社区 I test it (item.Artist.Name), I don't get anything. the artist property is null! I don't get it. why? do I even do it correctly? (I'm trying to get the artist's name, that's all)

And last question: I also have in the author's class a List (list of his songs). how should I load songs of him there?


What do you mean? Store your data? Your data is normally saved in a database that you chose in your connectionString. The initializer will take care of the database creation, seeding etc.

Selecting a song with his artist

You can get the Artist by including him in the query. For example:

 yourContext.Songs.Include("Artist").toList()' 

This will return all the songs and the artist will be filled in for each song.

Selecting a song with his artist

In the same way you fill in the Artist for the song.

yourContext.Artists.Include("ListPropertyName").toList()

Here is a link that might help you:

http://msdn.microsoft.com/en-us/library/bb896272.aspx

Saving a song together with his artist.

I'm going to show the way I work , Your relationship should be something like this:

public class Artist
    {
        [Key]
        public Guid ID { get; set; }

        //..OtherProperties ...

        public virtual ICollection<Songs> Songs { get; set; }
    }

public class Song
    {
        [Key]
        public Guid ID { get; set; }

        //..OtherProperties ...


        //Foreign Key Stuff
        [ForeignKey("Artist")]
        public Guid ArtistId { get; set; }

        //Relationship
        public virtual Artist Artist { get; set; }
    }

If you work this way you can just fill in the ArtistId manually in the song. So You will have to do the following:

var Artists= new List<Artist>()
        {
            new Artist{ Name = "ArtistName", Birthday=DateTime.Parse("4-4-1988"), ID = Guid.NewGuid()}
        };
        Authors.ForEach(s => context.Authors.Add(s));

        var Songs= new List<Song>()
        {
            new Song { Name="SongName", Genre = Genre.GenreTypes.Rock, ArtistId=Artists[0].ID} //<--
        };
        Songs.ForEach(s => context.Songs.Add(s));

        context.SaveChanges();

Now, what is the advance with separating the ID and the actual Artist. It's very simple, when you make a Artist you can manually set the ID and you can use it later in your code instead of saving the artist first. So first you make your artists and set there Id and then you can directly use it in your song. In your example you actually do Savechanges() twice. If you're using 'my' method you only have to save once. You should really check some tutorials on Entity Framework , google is your friend :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜