简体   繁体   中英

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

I have the 2 tables in the picture below and want to add a foreign key relationship between them with the ServiceID being the foreign key

My 2 tables side by side My Database Context

your code can like this:

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

also you get all info about relations in this link: Relation in EF

Add the following to ServiceStatistics:

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

public int ServiceLogRefId {get; set; }

May be a slight difference in annotation given version (you haven't specified in your question).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM