Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 EF4.0
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:
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:
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 4-Dec-12 1:35am
Edited 4-Dec-12 1:41am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
public virtual ICollection<user> UsersWhoRead {get;set;}</user>
 
As concerns mappings, i have added few lines of code:
this.HasMany(x => x.ReadMessages).WithMany(x => x.UsersWhoRead)
            .Map(m => m.MapLeftKey("User_Key").MapRightKey("Message_Key").ToTable("UserReadMessage"));
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,696
1 Sergey Alexandrovich Kryukov 6,675
2 CPallini 5,315
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100