Edit: full source code now available at https://bitbucket.org/moerie/multilanguagestring
I’ve implemented a class called MultilingualString which can serialize a dictionary to XML and be saved to the database.
(See source here: https://gist.github.com/4491990 )
This is actually an implementation of IDictionary but I don’t actually add that to my class as it seems to cause hiccups for the Entity Framework.
usage of this class is like follows:
Suppose a class Product is defined like this:
public class Product
{
public int Id { get; set; }
public virtual MultilingualString Description { get; set; }
public virtual MultilingualString Name { get; set; }
}
Then you can do the following:
// make some products
var product1 = new Product
{
Id = 1,
Name = new MultilingualString(),
Description = new MultilingualString()
};
product1.Name["fr"] = "Produit 1";
product1.Description["fr"] = "Déscription 1";
product1.Name["nl"] = "Produkt 1";
product1.Description["nl"] = "Omschrijving 1";
Anyway, since I only want the Id and the Xml value I made an EntityTypeConfiguration:
public class MultilingualStringConfiguration: EntityTypeConfiguration<MultilingualString>
{
public MultilingualStringConfiguration()
{
Ignore(m => m.Count);
Ignore(m => m.Keys);
Ignore(m => m.Values);
Ignore(m => m.IsReadOnly);
}
}
When I run this code, the MultilingualStrings table is created as followed:
CREATE TABLE [dbo].[MultilingualStrings](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Value] [nvarchar](max) NULL,
[Item] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.MultilingualStrings] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
As you can see, the Entity Framework adds a property called ‘Item’, and I suspect this is because of the following property in MultilingualString:
/// <summary>
/// Gets or sets the element with the specified key.
/// </summary>
/// <returns>
/// The element with the specified key.
/// </returns>
/// <param name="key">The key of the element to get or set.</param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception><exception cref="T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and <paramref name="key"/> is not found.</exception><exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
public string this[string key]
{
get
{
if (ContainsKey(key))
return Translations[key];
return string.Empty;
}
set
{
Translations[key] = value;
UpdateValue();
}
}
This is the property that allows my MultilingualString property to be used as a Dictionary, but sadly I can’t seem to reach this property from the EntityTypeConfiguration.
How can I ignore this property? Thanks in advance.
P.S. For those of you interested in the MultilingualString class, note that you will need the LinqKit library (and specifically the AsExpandable() and Compile() functions to perform Linq queries with this class.
Full source code:
https://bitbucket.org/moerie/multilanguagestring
Since this MultilingualString is in fact a property, I tried to resolve my problem by just mapping my MultilingualString to a String property. Then I could just tell the Entity Framework to not map my MultilingualString property entirely.
This is my product class now:
This also seriously simplifies my database.
Yes, I couldn’t find a fluent API solution for my original problem, but this workaround turned out to be excellent.