我们需要在 Fluent Api 中定义所有模型吗?
do we need to define all Models in Fluent Api?
在 .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; }
}
}
when we really need to define the relationship between models with Fluent API
当您不遵守约定,或者当您的预期用途超出这些约定时。
模型构建器可以推断出很多常用的意图,但是例如非可选的一对一关系需要使用属性或流畅的配置来定义。
在 .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; }
}
}
when we really need to define the relationship between models with Fluent API
当您不遵守约定,或者当您的预期用途超出这些约定时。
模型构建器可以推断出很多常用的意图,但是例如非可选的一对一关系需要使用属性或流畅的配置来定义。