代码之家  ›  专栏  ›  技术社区  ›  nightingale2k1

我们需要用fluent api定义所有模型吗?

  •  0
  • nightingale2k1  · 技术社区  · 6 年前

    在.NET教程(在UDMEY和一些站点)中,我看到他们声明了用于定义模型之间关系的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; }
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   CodeCaster    6 年前

    当我们真正需要用fluent api定义模型之间的关系时

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

    modelbuilder可以推断许多常用的意图,但例如,需要使用属性或fluent配置来定义非可选的一对一关系。