[英]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.