实体框架4 POCO与词典 [英] Entity Framework 4 POCO with Dictionary

查看:146
本文介绍了实体框架4 POCO与词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个POCO(普通老CLR对象)

  public Foo 
{
public virtual int Id {get;组; }
public virtual Dictionary< string,string>东西{get;组; }
public virtual string更多{get;组; }
}

使用 model first 方法(即我还没有数据模型),我将如何处理持续的Stuff(Dictionary)? p>

解决方案

这不是问题的真实答案,但由于没有其他答复,我会分享我所做的。 / p>

我只是创建了一个新的类型 {Id,Code,Text} ,并用该类型的列表替换了我的字典。然后我做这样的事情来获取密钥,值或者做一个查找:

 列表< string>文本=(从s中的sv。可选值选择sv.Text).ToList(); 
列表< string>代码=(从s中的sv。可选值选择sv.Code).ToList();
string text =(from sv in q.SelectableValues where sv.Code ==MyKeyselect sv.Text).First();

在我的情况下,字典中的条目数量往往很小。但是,当字典/列表较大时,请参阅此问题以获取性能注意事项。


I have a POCO (Plain Old CLR Object)

public Foo
{
   public virtual int Id { get; set; }
   public virtual Dictionary<string, string> Stuff { get; set; }
   public virtual string More { get; set; }
}

Using the model first approach (i.e. I don't have a data model yet), how would I handle persisting Stuff (Dictionary)?

解决方案

This is not a true answer to the question, but since there are no other replies, I'll share what I did.

I simply created a new type {Id, Code, Text} and replaced my dictionary with a list of that type. I then do something like this to get the keys, values, or do a lookup:

List<string> texts = (from sv in q.SelectableValues select sv.Text).ToList();
List<string> codes = (from sv in q.SelectableValues select sv.Code).ToList();
string text = (from sv in q.SelectableValues where sv.Code == "MyKey" select sv.Text).First();

In my case the number of entries in the dictionary tends to be small. However, see this question for performance considerations when the dictionary/list is large.

这篇关于实体框架4 POCO与词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆