I have HomeSetting table with duplicate HomeIds and I want to remove it by setting the foreign key as well.
public class Home
{
[Key]
public int Id { get; set; }
[Required]
[Column(TypeName = "NVARCHAR(200)")]
public string HomeId { get; set; }
[Required]
[Column(TypeName = "NVARCHAR(200)")]
public string HomeName { get; set; }
}
public class HomeSetting
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column(TypeName = "NVARCHAR(200)")]
public string HomeId { get; set; }
[Column(TypeName = "NVARCHAR(MAX)")]
public string Settings { get; set; }
}
I added [ForeignKey("Home")] attribute to HomeSetting's table HomeId column but it doesn't work. How do i mange this using ef core migration ?
[ForeignKey("Home")]
public string HomeId { get; set; }
1条答案
按热度按时间s8vozzvw1#
You must use key for foreign key and cant use property for foreign key.
In this moment we have two way:
actually first way is simple I write second way:
OnModelCreateing
method in DBContext