C# 实体框架6编码一对一实体关系的首个fluent api配置

C# 实体框架6编码一对一实体关系的首个fluent api配置,c#,.net,entity-framework-6,C#,.net,Entity Framework 6,我目前在两个实体之间有一对一的关系。主要实体如下所示: public class PrimaryEntity { // primary key public Guid Id { get; set; } // some other properties... public RelatedEntity Related { get; set; } } public class RelatedEntity { // both the primary and foreign

我目前在两个实体之间有一对一的关系。主要实体如下所示:

public class PrimaryEntity
{
   // primary key
   public Guid Id { get; set; }
   // some other properties...
   public RelatedEntity Related { get; set; }
}
public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   // some other properties
}
我的相关实体如下所示:

public class PrimaryEntity
{
   // primary key
   public Guid Id { get; set; }
   // some other properties...
   public RelatedEntity Related { get; set; }
}
public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   // some other properties
}
所以我的问题是,如何使用EF6 fluent api定义这种一对一的关系(不能使用注释)?为此,我尝试了许多不同的配置设置,但都没有成功。甚至有可能定义这样的单边关系,并使键名称不同吗?是因为我的RelatedEntity对主键和外键使用相同的属性吗

感谢您的帮助

更新:


我能够解决这个问题。罪魁祸首在我设置Related=newrelatedEntity的PrimaryEntity的构造函数中。我想英孚不喜欢这样。我发现提供的3个答案在其他方面都差不多。谢谢大家的帮助。

是的,您可以使用EF fluent api配置一对一关系

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Configure StudentId as PK for StudentAddress
    modelBuilder.Entity<StudentAddress>()
        .HasKey(e => e.StudentId);

    // Configure StudentId as FK for StudentAddress
    modelBuilder.Entity<Student>()
                .HasOptional(s => s.StudentAddress) // Mark StudentAddress is optional for Student
                .WithRequired(ad => ad.Student); // Create inverse relationship

}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//将StudentId配置为StudentAddress的主键
modelBuilder.Entity()
.HasKey(e=>e.StudentId);
//将StudentId配置为StudentAddress的FK
modelBuilder.Entity()
.has可选(s=>s.StudentAddress)//标记StudentAddress对于学生是可选的
.WithRequired(ad=>ad.Student);//创建反向关系
}

摘自

是,您可以配置与EF fluent api的一对一关系

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Configure StudentId as PK for StudentAddress
    modelBuilder.Entity<StudentAddress>()
        .HasKey(e => e.StudentId);

    // Configure StudentId as FK for StudentAddress
    modelBuilder.Entity<Student>()
                .HasOptional(s => s.StudentAddress) // Mark StudentAddress is optional for Student
                .WithRequired(ad => ad.Student); // Create inverse relationship

}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//将StudentId配置为StudentAddress的主键
modelBuilder.Entity()
.HasKey(e=>e.StudentId);
//将StudentId配置为StudentAddress的FK
modelBuilder.Entity()
.has可选(s=>s.StudentAddress)//标记StudentAddress对于学生是可选的
.WithRequired(ad=>ad.Student);//创建反向关系
}

摘自

是,您可以配置与EF fluent api的一对一关系

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Configure StudentId as PK for StudentAddress
    modelBuilder.Entity<StudentAddress>()
        .HasKey(e => e.StudentId);

    // Configure StudentId as FK for StudentAddress
    modelBuilder.Entity<Student>()
                .HasOptional(s => s.StudentAddress) // Mark StudentAddress is optional for Student
                .WithRequired(ad => ad.Student); // Create inverse relationship

}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//将StudentId配置为StudentAddress的主键
modelBuilder.Entity()
.HasKey(e=>e.StudentId);
//将StudentId配置为StudentAddress的FK
modelBuilder.Entity()
.has可选(s=>s.StudentAddress)//标记StudentAddress对于学生是可选的
.WithRequired(ad=>ad.Student);//创建反向关系
}

摘自

是,您可以配置与EF fluent api的一对一关系

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Configure StudentId as PK for StudentAddress
    modelBuilder.Entity<StudentAddress>()
        .HasKey(e => e.StudentId);

    // Configure StudentId as FK for StudentAddress
    modelBuilder.Entity<Student>()
                .HasOptional(s => s.StudentAddress) // Mark StudentAddress is optional for Student
                .WithRequired(ad => ad.Student); // Create inverse relationship

}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//将StudentId配置为StudentAddress的主键
modelBuilder.Entity()
.HasKey(e=>e.StudentId);
//将StudentId配置为StudentAddress的FK
modelBuilder.Entity()
.has可选(s=>s.StudentAddress)//标记StudentAddress对于学生是可选的
.WithRequired(ad=>ad.Student);//创建反向关系
}

取自
RelatedEntity
类中的,您还需要定义一个导航属性,该属性指向
PrimaryEntity
类:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}
然后,您可以使用它在自定义
DbContext
类的
OnModelCreating
方法中配置一对一关系:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}

RelatedEntity
类中,您还需要定义一个导航属性,该属性指向
PrimaryEntity
类:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}
然后,您可以使用它在自定义
DbContext
类的
OnModelCreating
方法中配置一对一关系:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}

RelatedEntity
类中,您还需要定义一个导航属性,该属性指向
PrimaryEntity
类:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}
然后,您可以使用它在自定义
DbContext
类的
OnModelCreating
方法中配置一对一关系:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}

RelatedEntity
类中,您还需要定义一个导航属性,该属性指向
PrimaryEntity
类:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}
然后,您可以使用它在自定义
DbContext
类的
OnModelCreating
方法中配置一对一关系:

public class RelatedEntity
{
   // both the primary and foreign key
   public Guid PrimaryEntityId { get; set; }

   public PrimaryEntity Primary { get; set; }
   // some other properties
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<RelatedEntity>().HasKey(e => e.PrimaryEntityId);    
    modelBuilder.Entity<RelatedEntity>().HasRequired(re => re.Primary).WithOptional(pe => pe.Related);
}
公共类主实体
{
//主键
公共Guid Id{get;set;}
//其他一些属性。。。
公共关系实体相关{get;set;}
}
公共类关联实体
{
//主键和外键
公共Guid PrimaryEntityId{get;set;}
//其他一些属性
}
//映射
//如上所述配置主键
modelBuilder.Entity()
.HasKey(t=>t.PrimaryEntityId);
modelBuilder.Entity()
.HasRequired(p=>p.相关)
.WithRequiredPrincipal()//用于单面导航的空括号。
未经测试-从头开始…

公共类PrimaryEntity
{
//主键
公共Guid Id{get;set;}
//其他一些属性。。。
公共关系实体相关{get;set;}
}
公共类关联实体
{
//主键和外键
公共Guid PrimaryEntityId{get;set;}
//其他一些属性
}
//映射
//如上所述配置主键
modelBuilder.Entity()
.HasKey(t=>t.PrimaryEntityId);
modelBuilder.Entity()
.HasRequired(p=>p.相关)
.WithRequiredPrincipal()//用于单面导航的空括号。
未经测试-从头开始…

公共类PrimaryEntity
{
//主键
公共Guid Id{get;set;}
//其他一些属性。。。
公共关系实体相关{get;set;}
}
公共类关联实体
{
//主键和外键
公共Guid PrimaryEntityId{get;set;}
//其他一些属性
}
//