none
How to add 2 foreign keys and their corresponding navigation properties to a model RRS feed

  • Question

  • Hi all

    I a newcomer in asp.net mvc and i got stuck with a trivial problem for hours. I have a simple class that models a message. its here:

    public class Message
    {
        [Key]
        public int Id { get; set; }
    
        // AspNetUsers
        public ApplicationUser Sender { get; set; }
        public ApplicationUser Receiver { get; set; }
    
        // SenderId of the message
        [Required]
        [ForeignKey("Sender")]
        [Display(Name = "Sender")]
        [InverseProperty("MessageSenderId")]
        public string SenderId { get; set; }
    
        // ReceiverId of this message
        [Required]
        [ForeignKey("Receiver")]
        [Display(Name = "Receiver")]
        [InverseProperty("MessageReceiverId")]
        public string ReceiverId { get; set; }
    
        // other properties
    }

    The SenderId and ReceiverId are the Id of sender and receiver of this message respectively. The Sender and Receiver are navigation properties to fetch them from database when i want to pass the model to view.

    Unfortunately, when i want to add a new migration, this error is raised: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Mails_dbo.AspNetUsers_ReceiverId". The conflict occurred in database "DbName", table "dbo.AspNetUsers", column 'Id'.

    What can i do? Thanks

    • Moved by Fei Hu Monday, February 5, 2018 6:20 AM ASP.NET related
    Sunday, February 4, 2018 9:52 AM

All replies

  • You can post at the below forum, because there is a MVC forum there.

    http://forums.asp.net/

    The ViewModel should not be involved with Entity Framework migrations that I know about.

    Sunday, February 4, 2018 10:34 AM
  • Hello Ehsan,

    Your issue is out of support of C# General Question forum which mainly discusses C# programming language, IDE, libraries, samples and tools.

    I suggest that you can consult your issue directly on ASP.Net forum for better solution and support.

    Thanks for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Monday, February 5, 2018 6:16 AM
    Monday, February 5, 2018 6:15 AM