正如和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類。
你在試圖塑造什麼 - 傢俱? –
這兩張圖片有兩組不同的表格。在第一種情況下,我試圖建立一個「單詞」,它可以有一個單一的「圖像」相關聯,而一個「圖像」可以屬於很多單詞。在第二種情況下,「用戶」可以有許多「詞」,而「詞」可以屬於許多「用戶」。 – JesseBuesking
關於你的第二個問題:http://stackoverflow.com/questions/762937/whats-the-difference-between-identifying-and-non-identifying-relationships –