English 中文(简体)
实体框架:流利的API在一到零或一之间重复使用外国钥匙
原标题:Entity Framework : Fluent API makes duplicate foreign keys in one to zero or one

I have some models and I want to have a "One to zero or one" association. I searched and tried to make the association using Fluent API. Here are my classes :

public partial class Bill
{

[Key]
public int Id { get; set; }
[Required]
public double Amount { get; set; }

public virtual DomainRegOrder DomainRegOrder { get; set; }

}

public partial class DomainRegOrder
{
[Key]
public int Id { get; set; }
[Required]
public string DomainName { get; set; }

public virtual Bill Bill { get; set; }
}

在我的数据信息类中:

public class DataContext : DbContext {

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

 modelBuilder.Entity<DomainRegOrder>()
 .HasOptional(x => x.Bill)
 .WithOptionalDependent();

 }

but when I Initialize the Database, in the Bills table, I will have two columns named "DomainRegOrder_Id" and "DomainRegOrder_Id1".

我试图在 " 强 " 比尔 " /强 " 实体中添加 " 强 " 目标 " /强 ",作为铃声:

public int? DomainRegOrderId { get; set; }

但是,在“传单”的表格上将有一个“强”的“主页/强”和“强”的“强”的“主页/强”。

I also changed .WithOptionalDependent() to .WithOptionalPrincipal() but no changes were made as far as I know!!

有人能帮我弄掉这些 复制的外国钥匙吗?

最佳回答

我相信你想要这样的东西:

modelBuilder.Entity<Bill>() 
  .HasOptional(x => x.DomainRegOrder) 
  .WithOptionalDependent(c => c.Bill).Map(p => p.MapKey("DomainRegOrderID"); 
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签