与Entity Framework Fluent API一对一关系 [英] One to one relationship with Entity Framework Fluent API

查看:83
本文介绍了与Entity Framework Fluent API一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble with reverse navigation on one of my entities.

我有以下两个对象:

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对象上的外键导航正常。我无法获得候选对象的反向导航正常工作(如果可能的话)。

Now my foreign key navigation on the CandidateData object works fine. I am having trouble getting the reverse navigation for the candidate object to work (if that's even possible).

这是我的OnModelCreating函数:

This is my OnModelCreating function:

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创建的。

It's close to working except in the database I get two columns that link to the CandidateId. I get the one I from the POCO object the I get another column Candidate_CandidateId I assume was created by the modelBuilder.

此刻我很安静。有人可以说明发生了什么吗?

I am quiet lost at the moment. Can someone please shed some light on what's going on?

推荐答案

一对一的问题....
问题是EF和CODE,首先是1:1 ,以便从属具有引用主体的主键。尽管可以用其他方式定义数据库,并且实际上可以使用数据库定义主数据库上的OPTIONAL FK。 EF首先在代码中对此限制进行了规定。不错,我认为...

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. 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. Fair Enough I think...

相反,这是:IS已经添加了一些意见,您可能会忽略,如果您不同意:-)

TRy this instead: IS have added a few opinions on the way which you may ignore if you disagree:-)

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



时,才使用MAP

}

}

这篇关于与Entity Framework Fluent API一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆