Entity framework 实体框架中连接2个表的Fluent API

Entity framework 实体框架中连接2个表的Fluent API,entity-framework,visual-studio-2019,asp.net-core-2.1,Entity Framework,Visual Studio 2019,Asp.net Core 2.1,下面我将使用fluentapi连接两个表 这些是我的实体: public class Personas { [Key] public int idPersona { get; set; } public String nombrePersona { get; set; } public String apellidoPersona { get; set; } public String loginPersona { get; set; } publ

下面我将使用fluentapi连接两个表

这些是我的实体:

public class Personas
{
    [Key]
    public int idPersona { get; set; }
    public String nombrePersona { get; set; }
    public String apellidoPersona { get; set; }
    public String loginPersona { get; set; }
    public int idRol { get; set; }
    public Rol rolPersona { get; set; }
}

public class Rol
{
    [Key]
    public int idRol{ get; set; }
    public String nombreRol { get; set; }
    public ICollection<Personas> personas { get; set; }
}
公共类角色
{
[关键]
公共int-idPersona{get;set;}
公共字符串nombrePersona{get;set;}
公共字符串apellidoPersona{get;set;}
公共字符串loginPersona{get;set;}
public int idRol{get;set;}
公共角色角色{get;set;}
}
公共类Rol
{
[关键]
public int idRol{get;set;}
公共字符串nomberrol{get;set;}
公共ICollection角色{get;set;}
}
这是我的DBcontext文件

 public class AppDBContext:DbContext
{
    public AppDBContext(DbContextOptions<AppDBContext> options):base(options) {}

    public DbSet<Personas> tPersonas { get; set; }
    public DbSet<Rol> tRol { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Rol>().HasMany(s => s.personas);
        base.OnModelCreating(modelBuilder);
    }
}
公共类AppDBContext:DbContext
{
公共AppDBContext(DbContextOptions

结果应该是这样的

第二个问题:文档建议使用“DbModelBuilder”而不是“ModelBuilder”类,但Visual Studio会自动完成“OnModelCreating”方法。由于某些原因,我不能使用“System.Data.Entity”,我只允许使用“Microsoft.EntityFrameworkCore”

请帮忙

编辑1:这是控制器代码

// GET api/<controller>/5
    [HttpGet("{id}")]
    public HttpResponseMessage Get(int id)
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        Personas persona = dbContext.tPersonas.FirstOrDefault(p => p.idPersona == id);
        if (persona != null)
        {
            response.Content = new StringContent(JsonConvert.SerializeObject(persona));
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return response;
        }
        else
        {
            response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            response.Content = new StringContent("error message");
            return response;
        }
    }
//获取api//5
[HttpGet(“{id}”)]
公共HttpResponseMessage获取(int id)
{
HttpResponseMessage response=新的HttpResponseMessage(HttpStatusCode.OK);
Personas persona=dbContext.tPersonas.FirstOrDefault(p=>p.idPersona==id);
if(persona!=null)
{
response.Content=newstringcontent(JsonConvert.SerializeObject(persona));
response.Content.Headers.ContentType=new System.Net.Http.Headers.MediaTypeHeaderValue(“应用程序/json”);
返回响应;
}
其他的
{
response=新的HttpResponseMessage(HttpStatusCode.BadRequest);
response.Content=newstringcontent(“错误消息”);
返回响应;
}
}

第一个问题:必须使用
.include()
包含相关数据

[HttpGet(“{id}”)]
EF核心的公共异步任务

对于EF 6(
DbModelBuilder
),您是否通过nuget安装了
EntityFramework
包?

发布您的控制器或返回JSONI刚刚编辑的方法,感谢您的时间我没有,我将尝试nugget安装查看我的更新,使用ActionResult,省去您的麻烦
[HttpGet("{id}")]
public async Task<ActionResult<Personas>> Get(int id)
{
    var persona = await dbContext.tPersonas.Inculde(x => x.rolPersona).SingleAsync(x => x.idPersona == id));

    if (persona == null)
    {
        return NotFound();
    }

    return persona;
}