I am using EF Core. I have a class with 3 columns primary key, and this is used on other class and add 1 new primary key (4 primary key) How to create the relationship on OnModelCreating
?
public class Head
{
public string Id1 { get; set; }
public string Id2 { get; set; }
public string Id3 { get; set; }
public List<Details> Details { get; set; }
}
public class Details
{
public string Id1 { get; set; } -- foreign key and primary key
public string Id2 { get; set; } -- foreign key and primary key
public string Id3 { get; set; } -- foreign key and primary key
public string Id4 { get; set; } -- primary key
}
I hope help me to resolve this problem using EF Core
Inside your OnModelCreating
method you can do something like this,
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Head>()
.HasMany(header => header.Details)
.WithMany()
.HasForeignKey(details => new { details.Id1, details.Id2, details.Id3 });
}
Here HasMany()
method and WithMany()
methods are used to configure a many-to-many relationship between the Head and Details entitites.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.