我试图在不同班级的List<Dock>
时填充Profile.MatchId == Dock.MatchId
。
这是两个类的精简版本:
public class Profile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProfileId { get; set; }
public int MatchId { get; set; }
public virtual List<Dock> Docks { get; set; } = new List<Dock>();
}
public class Dock
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DockId { get; set; }
public int MatchId { get; set; }
}
如何正确填充List<Dock> Docks
导航属性?我相信我需要通过modelBuilder
来解决这个问题 - 但是之前没有这样做过。
答案 0 :(得分:-1)
好时光!有一个非常简单的模型关系配置示例。
public class SomeContext : DbContext
{
public DbSet<Profile> Profiles { get; set; }
public DbSet<Dock> Docks { get; set; }
protected override Void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Dock>()
.HasOne(x => x.Profile)
.WithMany(x => x.Docks)
.HasForeignKey(x => x.ProfileId);
}
}
public class Profile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProfileId { get; set; }
public int MatchId { get; set; }
public virtual List<Dock> Docks { get; set; } = new List<Dock>();
}
public class Dock
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DockId { get; set; }
public int MatchId { get; set; }
public int ProfileId { get; set; }
public Profile Profile { get; set; }
}
答案 1 :(得分:-1)
实际上你无法获得List&lt;&gt;的值在Profile中码头,因为Profile和码头之间没有直接连接。
你可以试试这个。
public class Profile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProfileId { get; set; }
public virtual List<Dock> Docks { get; set; } = new List<Dock>();
}
public class Dock
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DockId { get; set; }
[ForeignKey("Profile")]
public int ProfileId { get; set; }
public Profile Profile {get; set; }
}