2013-08-30 139 views
1

我有3個實體:如何與現有字段建立一對一關係?

  • Foo
  • Bar
  • UniqueFooBar

FooBar都是實體如下:

public class Bar { 

    public int Id {get; set;} 

    // inverse nav property 
    public virtual UniqueFooBar UniqueFooBar {get; set;} 

} 

public class Foo { 

    public string Name {get; set;} 

    // inverse nav property 
    public virtual UniqueFooBar UniqueFooBar {get; set;} 
} 

UniqueFooBar是查找如下:

public class UniqueFooBar { 

    public string FooName {get; set;} 
    public int BarId {get; set;} 

    // nav properties 
    public virtual Foo Foo {get; set;} 
    public virtual Bar Bar {get; set;} 

} 

隨着約束:

  • Foo是獨特
  • 有一個一對一的關係,兩者FooBar
  • Foo名稱是PK

    T他流利的API如下:

    class UniqueFooBarConfiguration : EntityTypeConfiguration<UniqueFooBar> { 
        public UniqueFooBarConfiguration() { 
         // Define the tablename and schema 
         Map(entity => entity.ToTable("UniqueFooBars")); 
    
         //// Define non-conventional key 
         HasKey(fooBar => fooBar.FooName); 
    
         // Define FKs - 1-to-1 
         HasRequired(fooBar => fooBar.Foo) 
          .WithRequiredPrincipal(foo => foo.UniqueFooBar) 
          .Map(key => key.MapKey("FooName")); 
         HasRequired(fooBar => fooBar.Bar) 
          .WithRequiredPrincipal(bar => bar.UniqueFooBar) 
          .Map(key => key.MapKey("BarId")); 
         // -------------------------------- 
    
        } 
    
    } 
    

正在發生的事情是,FooName被添加到foo表BarId被添加托特他吧桌。

如果在流利的API配置UniqueFooBar中,我嘗試使用Foo的「名稱」屬性,則會出現該字段已存在的錯誤。如果我嘗試使用Bar的「Id」屬性,也會發生同樣的情況。

如何配置UniqueFooBar使FK到Foo.NameBar.Id成爲一對一的關係?

更新

  • 無論Foo也不BarUniqueFooBar的限制或要求。
  • 一個UniqueFooBar記錄需要FooNameBarId

這似乎並非是一樣How to declare one to one relationship using Entity Framework 4 Code First (POCO)

+1

的可能重複(http://stackoverflow.com/questions/3622572/how-to-declare-one-to- [如何使用實體框架4代碼優先(POCO)申報一對一的關係] one-relationship-using-entity-framework-4-code-first-poco) – User

+0

你看過http://stackoverflow.com/questions/15549680/how-do-i-establish-a-one-to-one- relationship-with-entity-framework-code-first? –

+0

@PaulZahra如果您可以總結該接受答案中的鏈接,那麼我會接受它:http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one -mapping/ 因爲附加了賞金,所以有一點總結會對複製相關的代碼片段很好。 – IAbstract

回答

1

here採取下面是如何可以實現一個之間的一個映射的示例兩個實體,將其推斷爲鏈接表,根據需要添加HasRequired。

可以指定WithRequiredPrincipal而不使用lambda,它允許您排除導航屬性並仍然獲得適當的一對一映射。

在OnModelCreating方法的重寫中,您可以使用DBModelBuilder參數定義關係。

public class Customer 
{ 
    public Customer() 
    { 
     Address = new Address(); 
    } 

    public Guid Id { get; set; } 
    public string Name { get; set; } 
    public Address Address { get; set; } 
} 

public class Address 
{ 
    public Guid Id { get; set; } 
    public string City { get; set; } 
    public string Country { get; set; } 
    public string Street { get; set; } 
} 

public class CustomerContext : DbContext 
{ 
    public IDbSet<Customer> Customers { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Customer>() 
       .HasRequired(x => x.Address) 
       .WithRequiredPrincipal(); 
     base.OnModelCreating(modelBuilder); 
    } 
} 
+0

感謝您多加解釋......我想我明白爲什麼*事情在以前沒有用過。如果我理解正確,因爲我有一個向前的導航屬性,我不需要在'WithRequiredPrincipal'方法中提供一個lambda;這是似乎有些混亂的地方。 – IAbstract

相關問題