Entity Framework: 无法将条目添加到交叉引用 table
Entity Framework: Unable to add entry to cross-ref table
在我的数据库中,我有以下 tables:
st_dictionary
id
[other fields]
st_language
id
[other fields]
st_dictionary_language
dictionary_id
language_id
当我创建这些(和其他)table 的实体模型时,Entity Framework 显得足够智能 'condense' st_dictionary_language
交叉引用 table 进入
public partial class st_dictionary
{
public virtual ICollection<st_language> st_language { get; set; }
[other members]
}
正如预期的那样,此 属性 填充了 st_language
个实体,这些实体通过交叉引用 table.
链接
然而,当我希望引用另一种语言到这个 st_dictionary
实体时,我 运行 遇到了一个问题:
daoDictionary.st_language.Add(new st_language { id = newLangID });
上面的代码抛出一个异常,其中列出了一系列验证错误,每个错误都解释了某些 st_language
属性不能为空。这些非空属性都存在于 st_language
DB table 中,而不存在于交叉引用 table.
中
那么如何在 st_dictionary_language
table 中添加新条目?
您必须添加您希望交叉引用的实际实体,而不是传入一个新对象(在您的示例中,您只填充 id
属性)。
看你的例子,大概是这样的:
var daoLanguage = [retrieve the st_language entity via the Entity Framework];
daoDictionary.st_language.Add(daoLanguage);
祝你好运!
在我的数据库中,我有以下 tables:
st_dictionary
id
[other fields]
st_language
id
[other fields]
st_dictionary_language
dictionary_id
language_id
当我创建这些(和其他)table 的实体模型时,Entity Framework 显得足够智能 'condense' st_dictionary_language
交叉引用 table 进入
public partial class st_dictionary
{
public virtual ICollection<st_language> st_language { get; set; }
[other members]
}
正如预期的那样,此 属性 填充了 st_language
个实体,这些实体通过交叉引用 table.
然而,当我希望引用另一种语言到这个 st_dictionary
实体时,我 运行 遇到了一个问题:
daoDictionary.st_language.Add(new st_language { id = newLangID });
上面的代码抛出一个异常,其中列出了一系列验证错误,每个错误都解释了某些 st_language
属性不能为空。这些非空属性都存在于 st_language
DB table 中,而不存在于交叉引用 table.
那么如何在 st_dictionary_language
table 中添加新条目?
您必须添加您希望交叉引用的实际实体,而不是传入一个新对象(在您的示例中,您只填充 id
属性)。
看你的例子,大概是这样的:
var daoLanguage = [retrieve the st_language entity via the Entity Framework];
daoDictionary.st_language.Add(daoLanguage);
祝你好运!