3

At start i wanted to mention that i've been fighting this thing for a few days and tried many of the answers more or less related to this problem. Yet I could not resolve it.

I have two classes that represent tables in a DB. These are the existing tables used by legacy application and I cannot change them.

Message can have multiple MessageRecipients.

Environment: MVC3, EF4.1

Classes are:

public class Message
{
    [ForeignKey("MessageReciepients")]
    public virtual int MessageID { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime Recieved { get; set; }
    [ForeignKey("User")]
    public int AuthorUserID { get; set; }

    //P\\ Navigation properties
    public virtual IList<MessageRecipient> MessageReciepients { get; set; }
    public virtual User User { get; set; }
}

public class MessageRecipient
{
    //[Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int MessageID { get; set; }
    public int UserID { get; set; }
    public bool Read { get; set; }
    public bool Important { get; set; }
    public bool Deleted { get; set; }
    public bool Destroyed { get; set; }

    //P\\ Navigation Properties
    public virtual User User { get; set; }
}

The error I have is:

The foreign key component 'MessageID' is not a declared property on type 'MessageRecipient'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.

How to correctly map these classes, relationships to load the recipients of a message?

I can add that the navigation property User works correctly for a Message and loads a User's data correctly.

I'm not too experienced with .NET and I learn while doing this. I tried some EF API config to map these i tried swearing at it, curse it, and been close to cry and pray at the same time. No Joy!!

I would really appreciate the help.

Alex P.
  • 3,697
  • 9
  • 45
  • 110
Pawel
  • 2,636
  • 1
  • 18
  • 14

2 Answers2

1

fill in the missing properties:

public class Message 
{
    public int MessageID { get; set; }
}

public class MessageRecipient 
{
    public int MessageID { get; set; }

    [ForeignKey("MessageID")]
    public Message Message { get; set; }
}
Alex P.
  • 3,697
  • 9
  • 45
  • 110
Kris Ivanov
  • 10,476
  • 1
  • 24
  • 35
  • btw, if you need standard relations between entity objects when you put reference of `Message` class inside `MessageRecipient` class, EF will make the proper associations automatically – Kris Ivanov Mar 09 '12 at 16:38
  • Thanks for the answer but the first property in a Mesage class is already MessageID – Pawel Mar 09 '12 at 16:40
  • I have done what you said but now i got {"Invalid column name 'Message_MessageID'.\r\nInvalid column name 'Message_MessageID'."} **{SELECT [Extent1].[MessageID] AS [MessageID], [Extent1].[UserID] AS [UserID], [Extent1].[Read] AS [Read], [Extent1].[Important] AS [Important], [Extent1].[Deleted] AS [Deleted], [Extent1].[Destroyed] AS [Destroyed], [Extent1].[Message_MessageID] AS [Message_MessageID] FROM [dbo].[Message_Recipients] AS [Extent1]}** – Pawel Mar 09 '12 at 16:59
  • since your DB is already existing, might be easier to import into model instead of creating the classes by hand – Kris Ivanov Mar 09 '12 at 17:11
1

It turned out that the problem was with the composite key that i needed to use and it all could be solved with some attributes:

This is how it looks now:

public class Message
{
    public int MessageID { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime Recieved { get; set; }

    [ForeignKey("User")]
    public int AuthorUserID { get; set; }

    //P\\ Navigation properties
    public virtual ICollection<MessageRecipient> MessageRecipients { get; set; }
    public virtual User User { get; set; }
}

public class MessageRecipient
{
    [Key, Column(Order=0), ForeignKey("User")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserID { get; set; }

    [Key, Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int MessageID { get; set; }

    public bool Read { get; set; }
    public bool Important { get; set; }
    public bool Deleted { get; set; }
    public bool Destroyed { get; set; }

    //P\\ Navigation Properties
    public virtual User User { get; set; }
}
Alex P.
  • 3,697
  • 9
  • 45
  • 110
Pawel
  • 2,636
  • 1
  • 18
  • 14