2011-12-11 114 views
2

我有一些基本的數據庫設計問題,我想在清理所有數據庫關係之前先清除它們。MySQL DB設計問題

問題1

我有兩個表,其中第一個將包含第二個的1個實例。我將使用實體框架,並且想要從table2訪問table1,並從table1訪問table2

我的想法是從table1table2,另一個從table2table1。添加從table1table2的鏈接似乎工作。但是,當我嘗試添加第二個鏈接(從table2table1)時,MySQL Workbench添加了兩個鏈接而不是一個。我想我做錯了什麼,因爲我的理解是它應該只添加一個鏈接。我的設計是錯誤的,還是應該刪除正在添加的第二個鏈接?

mysql workbench diagram

問題2

接下來,我想實現一個連接表。一個table1的實例可以有table2的許多實例,反之亦然,所以連接表似乎是實現這一目標的必要結構。 MySQL允許我創建Identifying RelationshipNon-Identifying Relationship,我不確定要使用哪一個。有任何想法嗎?

enter image description here

如果需要更多的澄清,請讓我知道。

+2

你在試圖塑造什麼 - 傢俱? –

+0

這兩張圖片有兩組不同的表格。在第一種情況下,我試圖建立一個「單詞」,它可以有一個單一的「圖像」相關聯,而一個「圖像」可以屬於很多單詞。在第二種情況下,「用戶」可以有許多「詞」,而「詞」可以屬於許多「用戶」。 – JesseBuesking

+0

關於你的第二個問題:http://stackoverflow.com/questions/762937/whats-the-difference-between-identifying-and-non-identifying-relationships –

回答

1

正如​​和JesseB所指出的,我認爲你需要的只是1..n的關係。

實體框架4.1(和POCO代碼前)所有你需要的是一張地圖,聲明此關係,就像

this.HasRequired(t => t.Image) 
    .WithMany(t => t.Words) 
    .HasForeignKey(d => d.ImageId); 

請在這裏找到一個完整的,工作代碼。如果啓動,它將創建一個包含所有需要的外鍵的數據庫。你會看到你需要的唯一外鍵是image_id,在words表中。實體框架能夠將Word的集合注入到任何Image對象中,而不依賴任何其他外鍵。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using WordAndImages.Entities; 
using System.Data.Entity; 
using System.Data.Entity.ModelConfiguration; 

namespace WordAndImages 
{ 

    public class Word 
    { 
     public int Id { get; set; } 
     public int ImageId { get; set; } 
     public virtual Image Image { get; set; } 
     public string Value { get; set; } 
    } 

    public class Image 
    { 
     public int Id { get; set; } 
     public virtual List<Word> Words { get; set; } 
     public string Value { get; set; } 
     public Image() 
     { 
      Words = new List<Word>(); 
     } 
    } 


    public class Context : DbContext 
    { 
     static Context() 
     { 
      Database.SetInitializer<Context>(null); 
     } 

     public DbSet<Word> Words { get; set; } 
     public DbSet<Image> Images { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Configurations.Add(new WordsMap()); 
     } 
    } 

    public class WordsMap : EntityTypeConfiguration<Word> 
    { 
     public WordsMap() 
     { 
      this.HasRequired(t => t.Image) 
       .WithMany(t => t.Words) 
       .HasForeignKey(d => d.ImageId); 
     } 
    } 


    class Program 
    { 
     static void Main(string[] args) 
     { 

      #region Saving a Word with an Image 

      var context = new Context(); 
      context.Database.Delete(); 
      context.Database.CreateIfNotExists(); 

      var word = new Word(); 
      word.Value = "I'm a word"; 

      var image = new Image(); 
      image.Value = "I'm an image"; 

      word.Image = image; 

      context.Words.Add(word); 
      context.SaveChanges(); 
      #endregion 

      #region Accessing an Image from a Word and viceversa 

      var context2 = new Context(); 

      var recovered_word = context2.Words.Where(w => w.Value == "I'm a word").FirstOrDefault(); 
      Console.WriteLine(string.Format("I'm the word '{0}' and my image is '{1}'", word.Value, word.Image.Value)); 

      var recovered_image = context2.Images.Where(w => w.Value == "I'm an image").FirstOrDefault(); 
      Console.WriteLine(string.Format("I'm the image '{0}' and one of my images is '{1}'", recovered_image.Value, recovered_image.Words.First().Value)); 

      Console.ReadLine(); 
      #endregion 
     } 
    } 
} 

對於many-to-many關係,只是使用地圖像

this.HasMany(a => a.Words) 
    .WithMany(z => z.Images) 
    .Map(m => 
    m.ToTable("Images_Words").MapLeftKey("Word_id").MapRightKey("Image_id")); 

和修改你的類如下

public class Word 
{ 
    public int Id { get; set; } 
    public virtual List<Image> Images { get; set; } 
    public string Value { get; set; } 
    public Word() 
    { 
     Images = new List<Image>(); 
    } 
} 

public class Image 
{ 
    public int Id { get; set; } 
    public virtual List<Word> Words { get; set; } 
    public string Value { get; set; } 
    public Image() 
    { 
     Words = new List<Word>(); 
    } 
} 

如果不與傳統數據庫的工作,我喜歡設計領域第一個對象,並讓ORM創建表和外鍵。

如果您希望從數據庫開始,請考慮可從Extension Manager下載的擴展實體框架電源工具CTP1:它能夠從數據庫生成POCO類。