【问题标题】:do we need to define all Models in Fluent Api?我们需要在 Fluent Api 中定义所有模型吗?
【发布时间】:2018-12-01 15:10:40
【问题描述】:

在 .Net 教程中(在 udemy 和一些网站中)我看到他们声明了用于定义模型之间关系的 Fluent API。

我尝试创建模型而不是在 Fluent API 中定义关系,似乎迁移工作并且我检查了 Mysql 外键也定义得很好。

我只需要知道,什么时候我们真的需要用 Fluent API 定义模型之间的关系 (OnModelCreating)

我的模特:

using System.Collections.Generic;

namespace CRSApp.API.Models
{
    public class UserGroup
    {
        public int Id { get; set; }
        public string GroupName { get; set; }

        ICollection<User> Users { get; set; }

        ICollection<UserGroupDetail> Details {get; set;}
    }
}


namespace CRSApp.API.Models
{
    public class UserGroupDetail
    {
        public int Id { get; set; }
        public int UserGroupId { get; set; }
        public int ModuleComponentId { get; set; }

        public UserGroup UserGroup { get; set; }
        public ModuleComponent ModuleComponent { get; set; }
    }
}

【问题讨论】:

标签: c# .net-core entity-framework-core ef-core-2.0


【解决方案1】:

当我们真的需要用 Fluent API 定义模型之间的关系时

当您不遵守约定时,或者当您的预期用途超出这些约定时。

模型构建器可以推断出很多常用的意图,但例如非可选的一对一关系需要使用属性或流畅的配置来定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多