C# 我们需要在Fluent Api中定义所有模型吗?
在udemy的.Net教程和一些网站上,我看到他们声明了用于定义模型之间关系的Fluent API 我试图在fluentapi中创建模型而不是定义关系,看起来迁移工作正常,并且我检查了Mysql外键是否定义良好 我只想知道,什么时候我们真的需要在ModelCreating上用Fluent API定义模型之间的关系 我的模型:C# 我们需要在Fluent Api中定义所有模型吗?,c#,.net-core,entity-framework-core,ef-core-2.0,C#,.net Core,Entity Framework Core,Ef Core 2.0,在udemy的.Net教程和一些网站上,我看到他们声明了用于定义模型之间关系的Fluent API 我试图在fluentapi中创建模型而不是定义关系,看起来迁移工作正常,并且我检查了Mysql外键是否定义良好 我只想知道,什么时候我们真的需要在ModelCreating上用Fluent API定义模型之间的关系 我的模型: using System.Collections.Generic; namespace CRSApp.API.Models { public class User
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; }
}
}
当我们真的需要用Fluent API定义模型之间的关系时
当您不遵守约定,或者您的预期用途超出这些约定时
modelbuilder可以推断出许多常用的意图,但例如,非可选的一对一关系需要使用属性或fluent配置来定义。EF将使用对流来生成关联,如果您没有明确声明,请查看