I have the following entities:
public class User : EntityBase
{
[Required]
[Key]
public Guid Id { get; set; }
public virtual ICollection<BlogEntry> BlogEntries { get; set; }
public virtual ICollection<BlogEntry> [Name?] { get; set; } // User favourite blog entries
}
public class BlogEntry : EntityBase
{
[Required]
[Key]
public Guid Id { get; set; }
public virtual User User { get; set; }
public virtual ICollection<User> [Name?] { get; set; } // Users who favourites blog entry
}
CREATE TABLE [dbo].[Favourites](
[BlogEntry_Id] [uniqueidentifier] NOT NULL,
[User_Id] [uniqueidentifier] NOT NULL,
)
If I was to follow EF code first namming conventions what propoerty names should in both cases? Also the name of the Favourites table should be UserFavourites?
If the problem is not related to the naming of the ICollection how should I indicate EF to load from the Favourites table the list of users who favorited the BlogEntry and the list of BlogEntrie(s) favourited by the user?
When you use Code First you should not create tables yourself: let the framework do it for you.
Just make an
ICollection<User>Property in theBlogEntryand anICollection<BlogEntry>in the User Entity. To start with, leave out theUserproperty fromBlogEntry. The Entity Framework will create the junction table “UsersBlogEntries” for you then.There is much more to it, of course, but this should get you started. SO is full of excellent answers on this subject.