开发者

ASP.NET MVC3 + ActiveRecord + HasMany + Dictionary

开发者 https://www.devze.com 2023-03-01 15:17 出处:网络
I\'m trying to get a Dictiona开发者_开发知识库ry data-structure in place for one of my ASP.NET MVC models. Essentially, I would like a hash-map of settings as key-value pairs. My code is similar to:

I'm trying to get a Dictiona开发者_开发知识库ry data-structure in place for one of my ASP.NET MVC models. Essentially, I would like a hash-map of settings as key-value pairs. My code is similar to:

[ActiveRecord]
public class Blog extends ActiveRecordBase<Blog> {
  [Property]
  public IDictionary<string, string> Settings { get; set; }
}

However, I'm at a loss on how to implement this. I assume that I would need a DB table like BlogSettings with a key and value column; but I'm not clear on how to do this without creating another model.

NHibernate/ActiveRecord complain that:

A HasMany with type Map requires that you specify an 'Index', use the Index property Blog.Settings 

I tried adding a dummy index (since I'm not clear on how to add indicies with ActiveRecord), like so:

  [Property(Index="")]
  public IDictionary<string, string> Settings { get; set; }

I didn't expect any change, but instead I got a second, different error message:

ActiveRecord tried to infer details about the relation Blog.Settings but it could not find information about the specified target type System.String. If you have mapped a Collection or Dictionary of value types, please make sure you have specified the Table property

My question is therefore: What do I need to (code/DB wise) to set up my ActiveRecord model with a Dictionary of string key/value pairs as a single field, without creating a new class to encapsulate key-value pairs?


You need a IUserType. Here's one that implements a IUserType very similar to what you need.

Then use the ColumnType property of the [Property] attribute to reference the fully qualified type name of your IUserType.

0

精彩评论

暂无评论...
验证码 换一张
取 消