Problemas de EF con las propiedades de navegación y el mapeo

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; }
}

El error que tengo es:

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!!

Apreciaría mucho la ayuda.

preguntado el 09 de marzo de 12 a las 16:03

I would really appreciate if you could give me more hints :) -

2 Respuestas

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:

Asi es como luce ahora:

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; }
}

Respondido 26 Abr '14, 20:04

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; }
}

Respondido 26 Abr '14, 20:04

btw, if you need standard relations between entity objects when you put reference of Message clase adentro MessageRecipient class, EF will make the proper associations automatically - kris ivanov

Thanks for the answer but the first property in a Mesage class is already MessageID - Pawel

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

since your DB is already existing, might be easier to import into model instead of creating the classes by hand - kris ivanov

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.