Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have come here with one question related to EF 4.3.1.
In my domain model I have 2 models: User and Message, each one has a primary key, called Key.
After some analyzing process I have figured out, that I need to provide some additional funcions to represent messages, were read by some user.

So as a result, I declared new property in my User model:
C#
public virtual ICollection<Message> ReadMessages { get; set; }


So in fact, I need to provide linking between these 2 domain models through a third table for example: ReadMessages.
(
But one thing to notice, this table must contain columns, which will display time when message was read.
And another one, user can have no read messages
).

How to achieve this with fluent API in EF??
Maybe there is some experienced people in EF, who can help me with such linking.

I think it can be achieved as follows:
C#
public class MessangerUserMap:EntityTypeConfiguration<user>
{
  public MessangerUserMap()
  {
    this.HasMany(x => x.ReadMessages).WithRequired().Map(m =>
                {    m.ToTable("ReadMesssages").MapKey("UserKey");
                     ??? how to add column ReadTime to table ReadMessages!! 
                });
  }
}
Posted
Updated 4-Dec-12 1:41am
v2

1 solution

I have successfully resolved such problem.
The crucial moment here was in creation of many-to-many relationships between this domain entities.

So as a result i have injected to each concrete domain model property(collection of related entity).
For example, in domain model of Message i have injected such property:
C#
public virtual ICollection<user> UsersWhoRead {get;set;}</user>


As concerns mappings, i have added few lines of code:
C#
this.HasMany(x => x.ReadMessages).WithMany(x => x.UsersWhoRead)
            .Map(m => m.MapLeftKey("User_Key").MapRightKey("Message_Key").ToTable("UserReadMessage"));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900