Home > Net >  EF core (MySql) scraffold DB-First not generate FK as class
EF core (MySql) scraffold DB-First not generate FK as class

Time:04-12

I develop .Net core API server using .net core 3.1 with Pomelo EF core (for connect Mysql Database)

I using Database First approach for fist release scraffold generate FK attribute as Object property (Ac & Temp)

    public partial class Devices
    {
        public int Id { get; set; }
        public string Mac { get; set; }
        public string Hostname { get; set; }
        public int? AcId { get; set; }
        public int? TempId { get; set; }
        public virtual Ac Ac { get; set; }
        public virtual Temp Temp { get; set; }
    }

But a few month later I re-generate after updated my DB. Scraffold not generate FK as object anymore (I'm not sure I have update Nuget Package or not) I get result look like

public partial class Device
{
    public int Id { get; set; }
    public string Mac { get; set; }
    public string Hostname { get; set; }
    public int? AcId { get; set; }
    public int? TempId { get; set; }
}

I try to googling to find how it been this way

I worry about my api result need to use .Include()

var target = await this.db.Devices.Include(x => x.Ac).Include(x => x.Temp).FirstOrDefaultAsync(x => x.Id == id.Value);
if (target != null)
{
    var viewModel = this.mapper.Map<DeviceViewModel>(target);
    return Ok(viewModel);
}

Anyone know this is my fault or EF scraffold is changed the way to generate class and if its normal how to use .Include()

Thank you.

CodePudding user response:

OK I found solution for my problem. It's my mistake I generate Schema and table by create Model on Mysql Workbench.

When generate schema default option are checked on Skip creation foreign key it's make Scraffold generate my expected result.

So just uncheck.

  • Related