EntityFramework Fluent API creates relationships without navigation properties

Is there a way to create a relationship between two classes using only Identifiers ?
In this example, I'm trying to create a One-to-many relationship between Companyand Employee(One Companyhas a lot Employee)

Here is my code:

public class Company
{
    public Guid Id { get; set; }
    public string CompanyName { get; set; }

}

and

public class Employee
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    public Guid CompanyId { get; set; }
}

Using the Fluent API . I can create relationships while I create a navigation property. I am looking for a way to link these two classes with Company.Idand Employee.CompanyIdwithout any navigation properties.

Thanks in advance.

+4
source share
1 answer

, , , " ", - FK :

using System;
using System.Data.Entity.Migrations;

public partial class CreateCompanyAndEmployee : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "Companies",
            c => new
                {
                    Id = c.Guid(nullable: false),
                    CompanyName = c.String(nullable: false, maxLength: 100)
                })
            .PrimaryKey(t => t.Id);

        CreateTable(
            "Employees",
            c => new
                {
                    Id = c.Guid(nullable: false),
                    Name = c.String(nullable: false, maxLength: 100),
                    CompanyId = c.Guid(nullable: false)
                })
            .PrimaryKey(t => t.Id);

        AddForeignKey("Employees", "CompanyId", "Companies", "Id");
        CreateIndex("Employees", "CompanyId");
    }

    //...
}

, LINQ-to-Entities, , , , , , :

var companiesWithEmployees = context.Companies
    .GroupJoin(context.Employees,
        company => company.Id,
        employee => employee.CompanyId,
        (company, employees) => new
        {
            Company = company,
            Employees = employees
        })
    .ToList();

employee.CompanyId, . EF , CompanyId . EF - .

companiesWithEmployees . Company IEnumerable<Employee> , .

, .

+5

All Articles