简体   繁体   English

与Entity Framework Fluent API的一对一关系

[英]One to one relationship with Entity Framework Fluent API

I'm having trouble with reverse navigation on one of my entities. 我在我的一个实体上进行反向导航时遇到问题。

I have the following two objects: 我有以下两个对象:

public class Candidate
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long CandidateId { get; set; } 
    ....

    // Reverse navigation
    public virtual CandidateData Data { get; set; }
    ...

    // Foreign keys
    ....
}

public class CandidateData
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long CandidateDataId { get; set; }

    [Required]
    public long CandidateId { get; set; }

    // Foreign keys
    [ForeignKey("CandidateId")]
    public virtual Candidate Candidate { get; set; }
}

Now my foreign key navigation on the CandidateData object works fine. 现在我在CandidateData对象上的外键导航工作正常。 I am having trouble getting the reverse navigation for the candidate object to work (if that's even possible). 我无法获得候选对象的反向导航(如果可能的话)。

This is my OnModelCreating function: 这是我的OnModelCreating函数:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<Candidate>()
        .HasOptional(obj => obj.Data)
        .WithOptionalPrincipal();

    base.OnModelCreating(modelBuilder);
}

It's close to working except in the database I get two columns that link to the CandidateId. 它接近工作,除了在数据库中我得到两个链接到CandidateId的列。 I get the one I from the POCO object the I get another column Candidate_CandidateId I assume was created by the modelBuilder. 我从POCO对象得到了一个我得到的另一列Candidate_CandidateId我假设是由modelBuilder创建的。

I am quiet lost at the moment. 此刻我很安静。 Can someone please shed some light on what's going on? 有人可以了解一下发生了什么吗?

The One to One problem.... The issue is EF and CODE First, when 1:1 , for the dependent to have a Primary key that refers to the principal. 一对一的问题......问题是EF和CODE首先,当1:1时,依赖者有一个引用主体的主键。 ALthough you can define a DB otherwise and indeed with a DB you can even have OPTIONAL FK on the Primary. 虽然您可以在其他方面定义数据库,但实际上也可以使用数据库定义数据库,您甚至可以在主数据库上定 EF makes this restriction in Code first. EF首先在Code中进行此限制。 Fair Enough I think... 公平够了我想......

TRy this instead: IS have added a few opinions on the way which you may ignore if you disagree:-) 相反,请试试:如果您不同意,IS已经对您可能忽略的方式添加了一些意见 :-)

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace EF_DEMO
{
class FK121
{
    public static void ENTRYfk121(string[] args)
    {
        var ctx = new Context121();
        ctx.Database.Create();
        System.Console.ReadKey();
    }
}
public class Candidate
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]// best in Fluent API, In my opinion..
    public long CandidateId { get; set; }
 //   public long CandidateDataId { get; set; }// DONT TRY THIS... Although DB will support EF cant deal with 1:1 and both as FKs
    public virtual CandidateData Data { get; set; }  // Reverse navigation

}
public class CandidateData 
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] // best in Fluent API as it is EF/DB related 
    public long CandidateDataId { get; set; }   // is also a Foreign with EF and 1:1 when this is dependent
   // [Required]
   // public long CandidateId { get; set; }   // dont need this... PK is the FK to Principal in 1:1
   public virtual Candidate Candidate { get; set; } // yes we need this
}
public class Context121 : DbContext
{
    static Context121()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Context121>());
    }
    public Context121()
        : base("Name=Demo") { }
    public DbSet<Candidate> Candidates { get; set; }
    public DbSet<CandidateData> CandidateDatas { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Candidate>();

        modelBuilder.Entity<CandidateData>()
                    .HasRequired(q => q.Candidate)
                    .WithOptional(p=>p.Data) // this would be blank if reverse validation wasnt used, but here it is used
                    .Map(t => t.MapKey("CandidateId"));    // Only use MAP when the Foreign Key Attributes NOT annotated as attributes
    }
}

} }

I think that the foreign key should be created as: .Map(t => t.MapKey("CandidateDataId")) because thsi foreign key will be placed in Candidate table... 我认为外键应该创建为:.Map(t => t.MapKey(“CandidateDataId”))因为thsi外键将被放置在Candidate表中...

Waht do you think? 你觉得呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Entity Framework Fluent API 的一对一可选关系 - One to one optional relationship using Entity Framework Fluent API 实体框架通过流畅的API继承了一对零的一对一关系配置 - Entity Framework inherited one to zero to one relationship configuration with fluent API 使用Fluent API在实体框架中创建一对多关系 - Creating one to many relationship in Entity Framework using Fluent API 在实体框架6流畅的api中的视图上映射多对一关系 - Mapping a many to one relationship over a view in entity framework 6 fluent api 用于一对一实体关系的Entity Framework 6代码第一个流利的api配置 - Entity Framework 6 code first fluent api config for one to one entity relationship 添加与表 Entity Framework 6 fluent api 的第二个一对一关系 - Adding a second one-to-one relationship with table Entity Framework 6 fluent api 实体框架代码First Fluent API配置,用于一对一的识别关系 - Entity Framework Code First Fluent API configuration for one to one identifying relationship 使用流利的api在实体框架中进行一对多映射 - One to many mapping in Entity framework with fluent api 使用流畅API的一对多实体框架 - Entity Framework one to many using fluent API 使用 Fluent API 使用 Entity Framework 6 在 asp.net 中配置一对多关系 - Configuring one to Many relationship in asp.net with Entity Framework 6 with Fluent API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM