Reputation: 45921
I'm developing a .NET Framework 4.5 library with C# and Entity Framework 6.1 Code First.
I have these two classes:
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public ICollection<Group> Groups { get; set; }
public ICollection<Message> MessagesSent { get; set; }
public ICollection<Message> MessagesReceived { get; set; }
}
public class Message
{
public int MessageId { get; set; }
public string Body { get; set; }
public DateTime DateSent { get; set; }
public User Sender { get; set; }
public ICollection<User> Recipients { get; set; }
}
How can I do to generate Recipient table?
I'm using Fluent API and I have two classes:
public class MessageConfiguration : EntityTypeConfiguration<Message>
AND
public class UserConfiguration : EntityTypeConfiguration<User>
But I don't what I have to do to generate Recipient
table.
I'm trying to represent this:
Is my database design correct?
Upvotes: 1
Views: 95
Reputation: 49095
Try this:
public class MessageConfiguration : EntityTypeConfiguration<Message>
{
public MessageConfiguration()
{
this.HasMany(x => x.Recipients)
.WithMany(x => x.MessagesReceived)
.Map(x => x.ToTable("MessageRecipients")
.MapLeftKey("MessageId")
.MapRightKey("UserId"));
}
}
(Please note that I set the table name to MessageRecipients
instead of Recipients
)
Upvotes: 1