Mike
Mike

Reputation: 773

How to skip the creation of a table for a parent entity that only serves as a bridge?

I have a domain model where many of the entities have content that needs to be localized. For example, we may have a Product entity where the name and the description need multi-language support. The same goes for many other entities.

Right now this is design I want to implement:

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; }
}

One thing I don't like is that the table for LocalizedContent will only have one column (Id). Its purpose is really to serve as a bridge between LocalizedContentEntity and the other tables. From a Db point of view all I really need is the LocalizedContentEntity table. Is there any way to map these entities without creating the one column table?

Upvotes: 1

Views: 34

Answers (1)

Richard Deeming
Richard Deeming

Reputation: 31198

Try making LocalizedContent a ComplexType:

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

This should result in the following tables (based on 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

Upvotes: 1

Related Questions