写表时如何在 Visual Studio 中创建外键关系?

How can I create a foreign key relationship in Visual Studio when writing out the tables?

我有下图中的2个表,想在它们之间添加外键关系,外键是ServiceID

My 2 tables side by side My Database Context

你的代码可以这样:

public class LoginLog
{
    .
    .
    .
    public int ServiceStateId{ get; set; }
    public ServiceState ServiceState{ get; set; }
}
public class ServiceState 
{
    .
    . 
    .
    public ICollection<LoginLog> LoginLogs{ get; set; }
}

您还可以在 link:Relation in EF

中获得有关关系的所有信息

将以下内容添加到 ServiceStatistics:

[ForeignKey("ServiceLogRefId")]
public ServiceLog ServiceLog {get; set; }

public int ServiceLogRefId {get; set; }

给定版本的注释可能略有不同(您未在问题中指定)。