'Fluent API equivalent for Data Annotation
I am not able to use DataAnnotations for setting key and foreign key as I am working on .NET framework. I need to confirm the fluentAPI equivalent of the same.
Below is what i tried, but says "The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint"
model:
public class Item
{
public string ItemNo{ get; set; }
public string parentItemNo{ get; set; }
// Adding below two navigation properties to existing model
public virtual Item Parent { get; set; }
public virtual ICollection<Item> Children { get; set; } = new List<Item>();
}
DBContext:
modelBuilder.Entity<Item>()
.HasKey(e => e.ItemNo )
.HasName("Item$0");
// Adding below lines to create foreign key using fluent API
entity.HasOne(e => e.Parent)
.WithMany(e => e.Children)
.HasForeignKey(e => e.ParentItemNo );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
