仅通过FluentAPI使用外键定义实体框架关系 [英] Define Entity Framework relationships using foreign keys only by FluentAPI

查看:85
本文介绍了仅通过FluentAPI使用外键定义实体框架关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过FluentAPI仅使用外键(无引用类型的虚拟属性)来定义Entity Framework关系(不应该更改数据模型)?

Is any way of defining Entity Framework relations using foreign keys only (no virtual properties of reference type) with FluentAPI (data models should not be changed)?

CardDataModel

public class CardDataModel
{
    public int CardId { get; set; }

}

CheckItemDataModel

public class CheckItemDataModel
{
    public int CheckItemId { get; set; }
    public int CardId { get; set; }
}


推荐答案

是的,可能EF核心。它不在EF6及更低版本中,但现在EF Core提供了 HasMany / HasOne 的无参数重载,从而可以配置关系:

Yes, it's possible in EF Core. It wasn't in EF6 and below, but now EF Core provides parameterless overloads of HasMany / HasOne which allow configuring such relationship:

modelBuilder.Entity<CardDataModel>()
    .HasMany<CheckItemDataModel>() // <-- note this
    .WithOne()
    .HasForeignKey(e => e.CardId);

这篇关于仅通过FluentAPI使用外键定义实体框架关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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