Fluent API 单关系模型
Fluent API Single Relation Model
虽然我对 C# 有很好的理解,但我对 Entity Framework 还很陌生,但在过去的 3 年里,我一直是一名 Web 开发人员 PHP。
我正在尝试将我的用户table与我的部门table联系起来。每次我 运行,我的 部门 属性 对于我的 用户 [=43] 只有 returns null =]
这是我的建造者
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>(entity =>
{
entity.HasOne(user => user.Department);
});
}
部门Class
[Table("department", Schema = "settings")]
public class Department
{
[Key]
public long id { get; set; }
public string department_name { get; set; }
public string department_code { get; set; }
}
用户Class
[Table("users", Schema = "settings")]
public class Users
{
[Key]
public long id { get; set; }
public string emp_id { get; set; }
public string username { get; set; }
public string password { get; set; }
public string salt { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string middle_initial { get; set; }
public string display_name { get; set; }
public string email { get; set; }
public long proj_id { get; set; }
[ForeignKey("Department")]
public long dept_id { get; set; }
public long job_id { get; set; }
public byte is_active { get; set; }
public DateTime? date_approved { get; set; }
public DateTime? date_created { get; set; }
public DateTime? birthday { get; set; }
public virtual Department Department { get; set; }
}
示例输出
{
"id": 11,
"emp_id": "100156",
"username": null,
"password": "dvser32Z2CaapKM=",
"salt": "o6k234RGOfasf=",
"first_name": "test",
"last_name": "sample",
"middle_initial": "M",
"display_name": "test test",
"email": null,
"proj_id": 7,
"dept_id": 1,
"job_id": 146,
"is_active": 1,
"date_approved": "2018-06-10T13:20:04.513",
"date_created": "2018-06-07T08:20:34.663",
"birthday": null,
"department": null
}
我真的很感激任何帮助,即使不是针对特定代码,而是针对其背后的概念本身。虽然最好在我理解这个概念的时候拥有它。
另外,我对一对一关系的理解是否正确?
提前致谢!
刚刚能够找出答案。有没有跟我一样笨的,求指点。
因此,当关系发生时,您必须指定您包含相关模型。所以..
_auth.Users.Include(u => u.Department);
你有三个选择。
- 正如 Siege21x 建议在您的查询中使用 include。
_auth.Users.Include(u => u.Department);
2.Use 每次写查询时加载相关字段的Load()方法
using (var context = new BloggingContext())
{
var user= context.Users
.Single(b => b.BlogId == 1);
context.Entry(user)
.Collection(b => b.Departments)
.Load();
}
3。使用延迟加载。为此安装 Microsoft.EntityFrameworkCore.Proxies nuget 包。然后将以下行添加到您的 dbContext class
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLazyLoadingProxies()
.UseSqlServer(myConnectionString);
您可以从官方获取更多信息website
虽然我对 C# 有很好的理解,但我对 Entity Framework 还很陌生,但在过去的 3 年里,我一直是一名 Web 开发人员 PHP。
我正在尝试将我的用户table与我的部门table联系起来。每次我 运行,我的 部门 属性 对于我的 用户 [=43] 只有 returns null =]
这是我的建造者
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>(entity =>
{
entity.HasOne(user => user.Department);
});
}
部门Class
[Table("department", Schema = "settings")]
public class Department
{
[Key]
public long id { get; set; }
public string department_name { get; set; }
public string department_code { get; set; }
}
用户Class
[Table("users", Schema = "settings")]
public class Users
{
[Key]
public long id { get; set; }
public string emp_id { get; set; }
public string username { get; set; }
public string password { get; set; }
public string salt { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string middle_initial { get; set; }
public string display_name { get; set; }
public string email { get; set; }
public long proj_id { get; set; }
[ForeignKey("Department")]
public long dept_id { get; set; }
public long job_id { get; set; }
public byte is_active { get; set; }
public DateTime? date_approved { get; set; }
public DateTime? date_created { get; set; }
public DateTime? birthday { get; set; }
public virtual Department Department { get; set; }
}
示例输出
{
"id": 11,
"emp_id": "100156",
"username": null,
"password": "dvser32Z2CaapKM=",
"salt": "o6k234RGOfasf=",
"first_name": "test",
"last_name": "sample",
"middle_initial": "M",
"display_name": "test test",
"email": null,
"proj_id": 7,
"dept_id": 1,
"job_id": 146,
"is_active": 1,
"date_approved": "2018-06-10T13:20:04.513",
"date_created": "2018-06-07T08:20:34.663",
"birthday": null,
"department": null
}
我真的很感激任何帮助,即使不是针对特定代码,而是针对其背后的概念本身。虽然最好在我理解这个概念的时候拥有它。
另外,我对一对一关系的理解是否正确?
提前致谢!
刚刚能够找出答案。有没有跟我一样笨的,求指点。
因此,当关系发生时,您必须指定您包含相关模型。所以..
_auth.Users.Include(u => u.Department);
你有三个选择。
- 正如 Siege21x 建议在您的查询中使用 include。
_auth.Users.Include(u => u.Department);
2.Use 每次写查询时加载相关字段的Load()方法
using (var context = new BloggingContext())
{
var user= context.Users
.Single(b => b.BlogId == 1);
context.Entry(user)
.Collection(b => b.Departments)
.Load();
}
3。使用延迟加载。为此安装 Microsoft.EntityFrameworkCore.Proxies nuget 包。然后将以下行添加到您的 dbContext class
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLazyLoadingProxies()
.UseSqlServer(myConnectionString);
您可以从官方获取更多信息website