我在其中一个实体上进行反向导航时遇到麻烦。

我有以下两个对象:

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; }
}

现在,我在CandidateData对象上的外键导航工作正常。我无法使候选对象的反向导航正常工作(如果可能的话)。

这是我的OnModelCreating函数:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

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

    base.OnModelCreating(modelBuilder);
}

除了在数据库中,我得到两列链接到CandidateId的列之外,它几乎可以正常工作。我从POCO对象中获得了一个,我又获得了另一列Candidate_CandidateId,我认为它是由modelBuilder创建的。

我现在安静地迷路了。有人可以说明发生了什么吗?

最佳答案

一对一问题....
问题是 EF 和 CODE First,当 1:1 时,依赖项具有引用主体的主键。尽管可以用其他方式定义数据库,并且实际上可以使用数据库定义主数据库上的OPTIONAL FK。 EF 首先在 Code 中进行了此限制。足够公平我认为...

试试这个: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
    }
}

}

关于c# - 与Entity Framework Fluent API一对一关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14809861/

10-12 13:24