Click here to Skip to main content
12,819,019 members (26,135 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# 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 2:35am
Updated 4-Dec-12 2:41am

1 solution

Rate this: bad
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"));

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2017
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