如何跳过为仅作为桥梁的父实体创建表?

时间:2012-12-05 19:04:48

标签: .net entity-framework localization

我有一个域模型,其中许多实体都有需要本地化的内容。例如,我们可能有一个Product实体,其名称和描述需要多语言支持。许多其他实体也是如此。

现在这是我想要实现的设计:

class LocalizedContent {
     public Guid Id {get; set; }
     public virtual ICollection<LocalizedContentEntry> Values {get; set;} 
}

class LocalizedContentEntry {
     public int Id {get; set; }
     public Guid ContentId {get; set; }
     public string Locale {get; set; }
     public string Value {get; set; } 
}

class Product {
     public int Id {get; set; }
     public LocalizedContent Name {get; set; }
     public LocalizedContent Description {get; set; } 
}

class OtherEntity {
     public int Id {get; set; }
     public LocalizedContent SomeColumn {get; set; }
}

我不喜欢的一件事是LocalizedContent的表只有一列(Id)。它的目的实际上是作为LocalizedContentEntity和其他表之间的桥梁。从Db的角度来看,我真正需要的是LocalizedContentEntity表。有没有办法在不创建一个列表的情况下映射这些实体?

1 个答案:

答案 0 :(得分:1)

尝试LocalizedContent ComplexType

[ComplexType]
public class LocalizedContent {
   public Guid Id { get; set; }
   public virtual ICollection<LocalizedContentEntry> Values { get; set;} 
}

这应该会产生以下表格(基于SQL CE):

CREATE TABLE "LocalizedContentEntries" (
   "Id" int not null identity,
   "ContentId" uniqueidentifier not null,
   "Locale" nvarchar(4000) null,
   "Value" nvarchar(4000) null,
   PRIMARY KEY ("Id")
);
CREATE TABLE "Products" (
   "Id" int not null identity,
   "Name_Id" uniqueidentifier not null,
   "Description_Id" uniqueidentifier not null,
   PRIMARY KEY ("Id")
);
CREATE TABLE "OtherEntities" (
   "Id" int not null identity,
   "SomeColumn_Id" uniqueidentifier not null,
   PRIMARY KEY ("Id")
);

http://blogs.msdn.com/b/adonet/archive/2009/05/28/poco-in-the-entity-framework-part-2-complex-types-deferred-loading-and-explicit-loading.aspx