Click here to Skip to main content
15,891,784 members
Home / Discussions / C#
   

C#

 
Questioncreate our own unit testing framework in C# Pin
Sakhalean26-Mar-23 18:32
Sakhalean26-Mar-23 18:32 
AnswerRe: create our own unit testing framework in C# Pin
OriginalGriff26-Mar-23 21:00
mveOriginalGriff26-Mar-23 21:00 
AnswerRe: create our own unit testing framework in C# Pin
jschell27-Mar-23 7:00
jschell27-Mar-23 7:00 
AnswerRe: create our own unit testing framework in C# Pin
Gerry Schmitz27-Mar-23 11:39
mveGerry Schmitz27-Mar-23 11:39 
GeneralRe: create our own unit testing framework in C# Pin
mtoha13-Apr-23 16:39
professionalmtoha13-Apr-23 16:39 
QuestionManagement of window coordinates and sizes for multiple screens of different resolutions Pin
temuco25-Mar-23 13:14
professionaltemuco25-Mar-23 13:14 
AnswerRe: Management of window coordinates and sizes for multiple screens of different resolutions Pin
OriginalGriff25-Mar-23 20:18
mveOriginalGriff25-Mar-23 20:18 
GeneralRe: Management of window coordinates and sizes for multiple screens of different resolutions Pin
temuco26-Mar-23 1:22
professionaltemuco26-Mar-23 1:22 
GeneralRe: Management of window coordinates and sizes for multiple screens of different resolutions Pin
Dave Kreskowiak26-Mar-23 5:38
mveDave Kreskowiak26-Mar-23 5:38 
GeneralRe: Management of window coordinates and sizes for multiple screens of different resolutions Pin
temuco26-Mar-23 22:28
professionaltemuco26-Mar-23 22:28 
GeneralRe: Management of window coordinates and sizes for multiple screens of different resolutions Pin
Dave Kreskowiak27-Mar-23 1:30
mveDave Kreskowiak27-Mar-23 1:30 
GeneralRe: Management of window coordinates and sizes for multiple screens of different resolutions Pin
temuco27-Mar-23 21:18
professionaltemuco27-Mar-23 21:18 
GeneralRe: Management of window coordinates and sizes for multiple screens of different resolutions Pin
Gerry Schmitz26-Mar-23 7:57
mveGerry Schmitz26-Mar-23 7:57 
QuestionEntity Framework Code First - two Foreign Keys from same table Pin
Alex Dunlop21-Mar-23 8:43
Alex Dunlop21-Mar-23 8:43 
I have the following models:

public class User
    {
        [Key]
        public int UserId { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
        public string? ImageUrl { get; set; }

        //Navigation properties
        public virtual List<Request> Requests { get; set; }
        public virtual List<Message> Messages { get; set; }
    }


public class Request
    {
        [Key]
        public int RequestId { get; set; }
        public DateTime RequestTime { get; set; } = DateTime.Now;
        public bool? AcceptStatus { get; set; }

        //Navigation properties
        public User TargetUserRef { get; set; }
        public User SenderUserRef { get; set; }
    }


I want the Request table to have two foreign keys. I tried to use the following fluent API:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Request>()
                        .HasOne(m => m.SenderUserRef)
                        .WithMany(t => t.Requests)
                        .HasForeignKey(m => m.RequestId)
                        .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Request>()
                        .HasOne(m => m.TargetUserRef)
                        .WithMany(t => t.Requests)
                        .HasForeignKey(m => m.RequestId)
                        .OnDelete(DeleteBehavior.Restrict);
        }


When I try to create migration, the following error is shown:

Quote:
Cannot create a relationship between 'User.Requests' and 'Request.TargetUserRef' because a relationship already exists between 'User.Requests' and 'Request.SenderUserRef'. Navigations can only participate in a single relationship. If you want to override an existing relationship call 'Ignore' on the navigation 'Request.TargetUserRef' first in 'OnModelCreating'.


How can I solve this problem?
AnswerRe: Entity Framework Code First - two Foreign Keys from same table Pin
Gerry Schmitz21-Mar-23 9:30
mveGerry Schmitz21-Mar-23 9:30 
AnswerRe: Entity Framework Code First - two Foreign Keys from same table Pin
jschell23-Mar-23 6:04
jschell23-Mar-23 6:04 
AnswerRe: Entity Framework Code First - two Foreign Keys from same table Pin
Richard Deeming27-Mar-23 0:35
mveRichard Deeming27-Mar-23 0:35 
QuestionHow to highlight all rows in listbox between two index values? Pin
Member 1405587917-Mar-23 1:25
Member 1405587917-Mar-23 1:25 
AnswerRe: How to highlight all rows in listbox between two index values? Pin
Pete O'Hanlon17-Mar-23 1:33
mvePete O'Hanlon17-Mar-23 1:33 
AnswerRe: How to highlight all rows in listbox between two index values? Pin
RedDk17-Mar-23 6:26
RedDk17-Mar-23 6:26 
QuestionMessage Closed Pin
15-Mar-23 20:32
Sivanujan Sivaraja15-Mar-23 20:32 
AnswerRe: Serial port listening application Pin
OriginalGriff15-Mar-23 21:02
mveOriginalGriff15-Mar-23 21:02 
GeneralRe: Serial port listening application Pin
Richard MacCutchan15-Mar-23 22:34
mveRichard MacCutchan15-Mar-23 22:34 
GeneralRe: Serial port listening application Pin
OriginalGriff15-Mar-23 22:41
mveOriginalGriff15-Mar-23 22:41 
GeneralRe: Serial port listening application Pin
Pete O'Hanlon16-Mar-23 0:19
mvePete O'Hanlon16-Mar-23 0:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.