导航属性和映射的EF问题(EF Problems with Navigation Properties and Mapping)

编程入门 行业动态 更新时间:2024-10-22 07:59:15
导航属性和映射的EF问题(EF Problems with Navigation Properties and Mapping)

一开始我想提一下,我已经和这个东西打了几天,并尝试了很多与这个问题或多或少相关的答案。 但我无法解决它。

我有两个代表DB中的表的类。 这些是遗留应用程序使用的现有表,我无法更改它们。

邮件可以有多个MessageRecipients。

环境:MVC3,EF4.1

课程是:

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

我遇到的错误是:

外键组件“MessageID”不是“MessageRecipient”类型的声明属性。 验证它是否未从模型中明确排除,并且它是有效的原始属性。

如何正确映射这些类,关系以加载邮件的收件人?

我可以添加导航属性User对Message的正确工作并正确加载User的数据。

我对.NET不太熟悉,而且我在这方面学习。 我尝试了一些EF API配置来映射这些我试过咒骂它,诅咒它,并且接近哭泣并同时祈祷。 没有喜悦!!

我真的很感激帮助。

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.

最满意答案

事实证明问题出在我需要使用的复合键上,所有这些都可以通过一些属性来解决:

这就是它现在的样子:

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

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

                    
                     
          

更多推荐

本文发布于:2023-08-03 12:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   EF   Problems   Mapping   Properties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!