使用Fluent API的EF Core中的一对一关系 [英] One to one relation in EF Core using Fluent API

查看:141
本文介绍了使用Fluent API的EF Core中的一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 EF Core 2.1

如何在EF Core中映射一对一关系.我有 Customer & Course 域实体,一位客户将拥有一门课程.

How to map one-to-one relationship in EF Core. I have Customer & Course domain entity where one customer will have one Course.

这是我的客户和服务方式CoursePOCO看起来像.

This is my how my Customer & CoursePOCO looks like.

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }

}

public class Course
{
    public int Id { get; set; }
    public string CouseName { get; set; }
    public virtual Customer Customer { get; set; }
}

我正在使用FluentAPI.

I am using FluentAPI.

 public class CourseConfiguration : IEntityTypeConfiguration<Course>
{
    public void Configure(EntityTypeBuilder<Parent> builder)
    {
        builder.HasKey(x => x.Customer.Id) //not allowing -> throws error
        //The properties expression 'x => Convert(x.Customer.Id, Object)' is not valid. 
        // The expression should represent a simple property access: 't => t.MyProperty'. 
        // When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'. 
        // Parameter name: propertyAccessExpression
    }
}

由于这是一对一的关系,所以我不想在Contact(FK -CustomerId)中创建额外的键,

Since it's one to one relation, I don't want to create an extra key in Contact (FK -CustomerId),

主要要求:-我想将 Id (在课程中)定义为 PK + FK &在这种关系中,客户是父实体.

Primary requirement:- I want to define Id (in Course) as PK + FK & in this relation Customer is parent entity.

就像我是基于配置的迁移一样,我将执行以下操作:-

Like if I was Configuration based Migration, I would do as follows:-

public class Course
{
    [Key]
    [ForeignKey("Customer")]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual Customer Customer { get; set; }
 }

我想在EF Core中使用Fluent API做同样的事情?

same thing I would like to do using Fluent API in EF Core??

谢谢!

推荐答案

其他答案指出,关键点是使用

As other answers point out, the key point is to use use the HasForeignKey<>() method to configure a foreign key.

但是请注意,应在从属实体而不是主体上设置外键.

详细信息:

客户

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Course Course {get;set;} 
}

,现在将 Course.Id 设置为引用 Customer.Id

public class AppDbContext : DbContext
{
    public AppDbContext (DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<Customer>(entity=>
        {
            entity.HasOne(customer=>customer.Course)
                .WithOne(course=> course.Customer)
                .HasForeignKey<Course>(course=>course.Id); 
        });
    }

    public DbSet<App.Models.Customer> Customer { get; set; }
    public DbSet<App.Models.Course> Courses{ get; set; }
}

生成的sql脚本是:

CREATE TABLE [Customer] (
    [Id] int NOT NULL IDENTITY,
    [Name] nvarchar(max) NULL,
    CONSTRAINT [PK_Customer] PRIMARY KEY ([Id])
);

GO

CREATE TABLE [Courses] (
    [Id] int NOT NULL,
    [CouseName] nvarchar(max) NULL,
    CONSTRAINT [PK_Courses] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Courses_Customer_Id] FOREIGN KEY ([Id]) REFERENCES [Customer] ([Id]) ON DELETE CASCADE
);

GO

这篇关于使用Fluent API的EF Core中的一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆