C# 使用EF Fluent API配置一对一关系

C# 使用EF Fluent API配置一对一关系,c#,entity-framework,entity-framework-6,ef-fluent-api,C#,Entity Framework,Entity Framework 6,Ef Fluent Api,因此,我希望在Person和Category之间建立一对一的关系 人员必须具有类别(FK) 如果Person表中仍然存在某些CategoryID,则删除Category将引发异常 如何通过EF FluentAPI实现这一点 我当前的代码: public class Person { public int PersonID { get; set; } public string Name { get; set; } public int CategoryID { get;

因此,我希望在
Person
Category
之间建立一对一的关系

  • 人员
    必须具有
    类别
    (FK)
  • 如果
    Person
    表中仍然存在某些
    CategoryID
    ,则删除
    Category
    将引发异常
  • 如何通过EF FluentAPI实现这一点

    我当前的代码:

    public class Person
    {
        public int PersonID { get; set; }
        public string Name { get; set; }
        public int CategoryID { get; set; }
        public virtual Category Category { get; set; }
    }
    
    public class Category
    {
        public int CategoryID { get; set; }
        public string Description { get; set; }
    }
    
    modelBuilder.Entity()
    .HasKey(pk=>pk.PersonID)
    .HasRequired(r=>r.CategoryID);
    
    请在上查看此MSDN文档

    由于导航属性仅在person对象上定义,因此您可以使用以下选项:

    modelBuilder.Entity<Person>()
                .HasKey(pk => pk.PersonID)
                .HasRequired(r => r.CategoryID);
    
    modelBuilder.Entity()
    .HasRequired(t=>t.Category)
    .WithRequiredPrincipal();
    
    请在上查看此MSDN文档

    由于导航属性仅在person对象上定义,因此您可以使用以下选项:

    modelBuilder.Entity<Person>()
                .HasKey(pk => pk.PersonID)
                .HasRequired(r => r.CategoryID);
    
    modelBuilder.Entity()
    .HasRequired(t=>t.Category)
    .WithRequiredPrincipal();
    
    使用以下方法:

    modelBuilder.Entity<Person>() 
        .HasRequired(t => t.Category) 
        .WithRequiredPrincipal();
    
    使用fluent api:

    public class Person
    {
       public int PersonID { get; set; }
       public string Name { get; set; }
    
       public virtual Category Category { get; set; }
    }
    
    public class Category
    {
       public int CategoryID { get; set; }
       public string Description { get; set; }
    
       public virtual Person Person{ get; set; }
    }
    
    模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity()
    .HasRequired(c=>c.Category)
    .With可选(c=>c.Person)
    .WillCascadeOnDelete(假);
    }
    
    使用以下方法:

    modelBuilder.Entity<Person>() 
        .HasRequired(t => t.Category) 
        .WithRequiredPrincipal();
    
    使用fluent api:

    public class Person
    {
       public int PersonID { get; set; }
       public string Name { get; set; }
    
       public virtual Category Category { get; set; }
    }
    
    public class Category
    {
       public int CategoryID { get; set; }
       public string Description { get; set; }
    
       public virtual Person Person{ get; set; }
    }
    
    模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity()
    .HasRequired(c=>c.Category)
    .With可选(c=>c.Person)
    .WillCascadeOnDelete(假);
    }
    
    这会将PersonID创建为PK吗?在这种情况下,您必须首先添加类别,然后插入person。PersonId的值必须等于CategoryId的值。这会将PersonId创建为PK吗?在这种情况下,您必须首先添加类别,然后插入person。PersonId的值必须等于CategoryId的值。