2016-09-23 61 views
0

我的問題很簡單:在這裏我有兩個一對多的關係類。 讓我們的樣本:從朱莉婭·勒曼的書採取「編程實體框架:代碼優先」 樣品實體框架:我應該提供雙面關係配置嗎?

public class Destination 
{ 
    public int DestinationId { get; set; } 
    public string Name { get; set; } 
    public string Country { get; set; } 
    public string Description { get; set; } 
    public byte[] Photo { get; set; } 
    public ICollection<Lodging> Lodgings { get; set; } 

    public Destination() 
    { 
     Lodgings = new List<Lodging>(); 
    } 
} 

public class Lodging 
{ 
    public int LodgingId { get; set; } 
    public string Name { get; set; } 
    public string Owner { get; set; } 
    public bool IsResort { get; set; } 
    public decimal MilesFromNearestAirport { get; set; } 
    public Destination Destination { get; set; } 
    public int DestinationId { get; set; } 
} 

這裏配置有FluentApi:

public class LodgingConfiguration : EntityTypeConfiguration<Lodging> 
{ 
    public LodgingConfiguration() 
    { 
     Property(p => p.Name).IsRequired().HasMaxLength(200); 
     HasRequired(p => p.Destination).WithMany(p => p.Lodgings); 
    } 
} 

public class DestinationConfiguration : EntityTypeConfiguration<Destination> 
{ 
    public DestinationConfiguration() 
    { 
     Property(p => p.Name).IsRequired().HasMaxLength(100); 
     Property(p => p.Description).HasMaxLength(500); 
     Property(p => p.Photo).HasColumnType("image"); 
     HasMany(p=>p.Lodgings).WithRequired(l=>l.Destination); 
    } 
} 

我想這行

HasRequired(p => p.Destination).WithMany(p => p.Lodgings); 

and

HasMany(p=>p.Lodgings).WithRequired(l=>l.Destination); 

提供目的地和住宿之間的關係相同的結果。

如果我只定義了其中一個規則,它也可以很好地工作。 在雙方定義相同的規則還是單方面聲明是好的是一個好習慣?

回答

1

下面是結果,如果你的三個變種做add-migration

,在酒店的映射和目標配置類型

public override void Up() 
{ 
    CreateTable(
     "dbo.Lodgings", 
     c => new 
      { 
       LodgingId = c.Int(nullable: false, identity: true), 
       Name = c.String(nullable: false, maxLength: 200), 
       Owner = c.String(), 
       IsResort = c.Boolean(nullable: false), 
       MilesFromNearestAirport = c.Decimal(nullable: false, precision: 18, scale: 2), 
       Destination_DestinationId = c.Int(nullable: false), 
      }) 
     .PrimaryKey(t => t.LodgingId) 
     .ForeignKey("dbo.Destinations", t => t.Destination_DestinationId, cascadeDelete: true) 
     .Index(t => t.Destination_DestinationId); 

    CreateTable(
     "dbo.Destinations", 
     c => new 
      { 
       DestinationId = c.Int(nullable: false, identity: true), 
       Name = c.String(nullable: false, maxLength: 100), 
       Country = c.String(), 
       Description = c.String(maxLength: 500), 
       Photo = c.Binary(storeType: "image"), 
      }) 
     .PrimaryKey(t => t.DestinationId); 

} 

從寄宿的配置類型刪除映射

public override void Up() 
{ 
    CreateTable(
     "dbo.Lodgings", 
     c => new 
      { 
       LodgingId = c.Int(nullable: false, identity: true), 
       Name = c.String(nullable: false, maxLength: 200), 
       Owner = c.String(), 
       IsResort = c.Boolean(nullable: false), 
       MilesFromNearestAirport = c.Decimal(nullable: false, precision: 18, scale: 2), 
       Destination_DestinationId = c.Int(nullable: false), 
      }) 
     .PrimaryKey(t => t.LodgingId) 
     .ForeignKey("dbo.Destinations", t => t.Destination_DestinationId, cascadeDelete: true) 
     .Index(t => t.Destination_DestinationId); 

    CreateTable(
     "dbo.Destinations", 
     c => new 
      { 
       DestinationId = c.Int(nullable: false, identity: true), 
       Name = c.String(nullable: false, maxLength: 100), 
       Country = c.String(), 
       Description = c.String(maxLength: 500), 
       Photo = c.Binary(storeType: "image"), 
      }) 
     .PrimaryKey(t => t.DestinationId); 

} 

距目的地配置類型

public override void Up() 
{ 
    CreateTable(
     "dbo.Lodgings", 
     c => new 
      { 
       LodgingId = c.Int(nullable: false, identity: true), 
       Name = c.String(nullable: false, maxLength: 200), 
       Owner = c.String(), 
       IsResort = c.Boolean(nullable: false), 
       MilesFromNearestAirport = c.Decimal(nullable: false, precision: 18, scale: 2), 
       Destination_DestinationId = c.Int(nullable: false), 
      }) 
     .PrimaryKey(t => t.LodgingId) 
     .ForeignKey("dbo.Destinations", t => t.Destination_DestinationId, cascadeDelete: true) 
     .Index(t => t.Destination_DestinationId); 

    CreateTable(
     "dbo.Destinations", 
     c => new 
      { 
       DestinationId = c.Int(nullable: false, identity: true), 
       Name = c.String(nullable: false, maxLength: 100), 
       Country = c.String(), 
       Description = c.String(maxLength: 500), 
       Photo = c.Binary(storeType: "image"), 
      }) 
     .PrimaryKey(t => t.DestinationId); 

} 

正如你可以看到,所產生的代碼(SQL服務器在這種情況下)去除的映射是完全一樣的。這總是一個風格問題,並保持一致。您可以顯式設置父級配置類型或任何其他樣式中子級的映射。

Here對這個話題是一個非常好的答案。

1

這兩種配置都是完美的罰款。如果你真的需要這是一個很好的做法。如果不保持關係簡單並且只暴露有意義的實體中的導航屬性。 (更關係到你的實體模型比EF問題)

1

只定義一次(例如)

modelBuilder.Entity<Student>() 
    .HasRequired<Course>(s => s.Course) 
    .WithMany(s => s.Students); 

記住,你需要保持你的代碼。在兩個單獨的表示/位置中擁有相同的信息是一個錯誤的祕訣。你認爲你的團隊中的每個人都會那麼細心嗎?