C# 如何在efcore中设计一对多关系?
嗨,我在实体框架核心上工作。我有用户表,用户可能是多个项目的一部分。每个项目的用户都必须输入时间表数据。例如,下面是我的用户表C# 如何在efcore中设计一对多关系?,c#,.net,ef-core-2.0,C#,.net,Ef Core 2.0,嗨,我在实体框架核心上工作。我有用户表,用户可能是多个项目的一部分。每个项目的用户都必须输入时间表数据。例如,下面是我的用户表 public class User { [Key] public string Id { get; set; } public string name { get; set; } public string emailId { get; set; } } 下面是我的项目表 public
public class User
{
[Key]
public string Id { get; set; }
public string name { get; set; }
public string emailId { get; set; }
}
下面是我的项目表
public class Project
{
[Key]
public string Id { get; set; }
public string Name { get; set; }
public string userId { get; set; }
}
在这里,用户可能属于多个项目。现在,对于每个项目,用户都必须输入时间表数据。下面是时间表
public class TimeSheetData
{
[Key]
public string id { get; set; }
public string project_id { get; set; }
public string hours_logged { get; set; }
}
我必须在实体框架核心中定义它。一个用户可能是多个项目的一部分,用户需要为每个项目的时间表输入数据。如何定义与上表相关的关系
在用户表中,我是否需要添加类似于
公共列表项目
?同样在项目表公共列表时间表中
我必须在这里定义一些东西。有人能帮我理解这一点吗?任何帮助都将不胜感激。谢谢假设您要将字符串更改为int-id,下面的操作是否有效
public class User
{
public User()
{
this.Projects = new HashSet<Project>();
}
[Key]
public int Id { get; set; }
public string name { get; set; }
public string emailId { get; set; }
public virtual ICollection<Project> Projects { get; set;}
}
public class Project
{
public Project()
{
this.TimeSheetData = new HashSet<TimeSheetData>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int userId { get; set; }
[ForeignKey("userId")]
public virtual User User {get; set; }
public virtual ICollection<TimeSheetData> TimeSheetData { get; set;}
}
public class TimeSheetData
{
[Key]
public int id { get; set; }
public int project_id { get; set; }
[ForeignKey("project_id")]
public virtual Project Project {get; set; }
public string hours_logged { get; set; }
}
公共类用户
{
公共用户()
{
this.Projects=newhashset();
}
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串emailId{get;set;}
公共虚拟ICollection项目{get;set;}
}
公共类项目
{
公共工程()
{
this.TimeSheetData=newhashset();
}
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
public int userId{get;set;}
[外键(“用户ID”)]
公共虚拟用户用户{get;set;}
公共虚拟ICollection时间表数据{get;set;}
}
公共类时间表数据
{
[关键]
公共int id{get;set;}
公共int项目_id{get;set;}
[外键(“项目id”)]
公共虚拟项目{get;set;}
公共字符串hours_logged{get;set;}
}
假设您将字符串更改为int-id,下面的操作是否有效
public class User
{
public User()
{
this.Projects = new HashSet<Project>();
}
[Key]
public int Id { get; set; }
public string name { get; set; }
public string emailId { get; set; }
public virtual ICollection<Project> Projects { get; set;}
}
public class Project
{
public Project()
{
this.TimeSheetData = new HashSet<TimeSheetData>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int userId { get; set; }
[ForeignKey("userId")]
public virtual User User {get; set; }
public virtual ICollection<TimeSheetData> TimeSheetData { get; set;}
}
public class TimeSheetData
{
[Key]
public int id { get; set; }
public int project_id { get; set; }
[ForeignKey("project_id")]
public virtual Project Project {get; set; }
public string hours_logged { get; set; }
}
公共类用户
{
公共用户()
{
this.Projects=newhashset();
}
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串emailId{get;set;}
公共虚拟ICollection项目{get;set;}
}
公共类项目
{
公共工程()
{
this.TimeSheetData=newhashset();
}
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
public int userId{get;set;}
[外键(“用户ID”)]
公共虚拟用户用户{get;set;}
公共虚拟ICollection时间表数据{get;set;}
}
公共类时间表数据
{
[关键]
公共int id{get;set;}
公共int项目_id{get;set;}
[外键(“项目id”)]
公共虚拟项目{get;set;}
公共字符串hours_logged{get;set;}
}
为什么要将字符串数据类型用于键?好的,我会将其更改为Int为什么要将字符串数据类型用于键?好的,我会将其更改为Int。谢谢,我会测试。假设项目类中的构造函数是Public project()对吗?而不是user?@Niranjan是的,谢谢你指出这一点。我已经更新了我的答案。请检查。如果我将用户id作为输入传递,如何获取特定用户的时间表数据_context.Users.Where(s=>s.Id==userid).Include(s=>s.Projects)在此之后如何包含时间表数据?嗨,山姆,我更改了所有内容并编译了,没有出现错误,但正在考虑如何为每个项目创建每个用户的时间表?我可以知道吗?\u context.Users.Include(s=>s.Projects.TimeSheetData)。其中(s=>s.Id==userid)。包括时间表数据时,项目将自动包括在内。上面的语句将返回所有项目,包括我将测试的给定用户的所有时间表数据。假设项目类中的构造函数是Public project()对吗?而不是user?@Niranjan是的,谢谢你指出这一点。我已经更新了我的答案。请检查。如果我将用户id作为输入传递,如何获取特定用户的时间表数据_context.Users.Where(s=>s.Id==userid).Include(s=>s.Projects)在此之后如何包含时间表数据?嗨,山姆,我更改了所有内容并编译了,没有出现错误,但正在考虑如何为每个项目创建每个用户的时间表?我可以知道吗?\u context.Users.Include(s=>s.Projects.TimeSheetData)。其中(s=>s.Id==userid)。包括时间表数据时,项目将自动包括在内。上述语句将返回所有项目,包括给定用户ID的所有时间表数据