简体   繁体   English

Foreign key for different named columns Entity Framework

[英]Foreign key for different named columns Entity Framework

public class Message
{
    [Key]
    public int MeesageId { get; set; }

    public int SenderId { get; set; }
    [ForeignKey("PersonId")]
    public virtual Person Sender { get; set; }

    public int ReceiverId { get; set; }
    [ForeignKey("PersonId")]
    public virtual Person Receiver { get; set; }

    public string Content { get; set; }
    public DateTime CreatedOn { get; set; }
    public bool Seen { get; set; }
}

public class Person
{
    public string Username { get; set; }
    [Key]
    public int PersonId { get; set; }
}

I'm getting this error:我收到此错误:

The ForeignKeyAttribute on property 'Receiver' on type 'Finder.Models.Message' is not valid.属性“接收器”上的fiender.models.message'上的forefenkeyattribute无效。 The foreign key name 'PersonId' was not found on the dependent type 'Finder.Models.Message'. The Name value should be a comma-separated list of foreign key property names.

What I think I should do is rename ReceiverId to PersonId , so it matches the foreign key, but then the property names would be too messy. Any help would be appreciated任何帮助,将不胜感激

The ForeignKey attribute specifies which int property is the foreign key for the specified navigation property.外国属性属性指定哪些int属性是指定导航属性的外键。 So所以

public class Message
{
        [Key]
        public int MeesageId { get; set; }

        public int SenderId { get; set; }
        [ForeignKey("SenderId")]
        public virtual Person Sender { get; set; }

        public int ReceiverId { get; set; }
        [ForeignKey("ReceiverId")]
        public virtual Person Receiver { get; set; }

        public string Content { get; set; }
        public DateTime CreatedOn { get; set; }
        public bool Seen { get; set; }
}

It is better to use fluent api.最好使用流利的api。

for example:例如:

    public class Message
    {
      public int MeesageId { get; set; }
   
        public int SenderId { get; set; }
       
        public virtual Person Sender { get; set; }
   
        public int ReceiverId { get; set; }
       
        public virtual Person Receiver { get; set; }
   
        public string Content { get; set; }
   
        public DateTime CreatedOn { get; set; }
   
        public bool Seen { get; set; }
    }
   
    public class Person
    {
        public string Username { get; set; }
    
        public int PersonId { get; set; }
   
        public List<Message> SenderMessages { get; set; }
   
        public List<Message> RecieverMessages { get; set; }
    }
   
    public class MessageConfigurations : IEntityTypeConfiguration<Message>
    {
        public void Configure(EntityTypeBuilder<Message> builder)
        {
            builder.HasKey(x => x.MeesageId);
   
            builder.HasOne(x => x.Sender)
                .WithMany(x => x.SenderMessages)
                .HasForeignKey(x => x.SenderId)
                .OnDelete(DeleteBehavior.NoAction);
   
   
            builder.HasOne(x => x.Receiver)
                .WithMany(x => x.RecieverMessages)
                .HasForeignKey(x => x.ReceiverId)
                .OnDelete(DeleteBehavior.NoAction); 
        }
    }
   
   
    public class PersonConfigurations : IEntityTypeConfiguration<Person>
    {
        public void Configure(EntityTypeBuilder<Person> builder)
        {
            builder.HasKey(x => x.PersonId);
   
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM