EF 迁移种子 - 无法创建“Application.Model.TranslationItem”类型的常量值
Posted
技术标签:
【中文标题】EF 迁移种子 - 无法创建“Application.Model.TranslationItem”类型的常量值【英文标题】:EF migration seed - Unable to create a constant value of type 'Application.Model.TranslationItem' 【发布时间】:2013-01-15 11:04:13 【问题描述】:TranslationItem 模型:
[DataContract]
public class TranslationItem : CloneableBaseEntity<TranslationItem>, IValidatableObject
public int TranslationId get; set;
public Translation Translation get; set;
public int Lcid get; set;
private string _text;
public string Text
get return _text;
set
_text = value;
RaisePropertyChanged("Text");
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
var t = validationContext.ObjectInstance as TranslationItem;
return t.Translation.Validate(validationContext);
翻译模型:
[DataContract]
public class Translation : CloneableBaseEntity<Translation>, IValidatableObject
private static int _englishLcid = 9;
private ObservableCollection<TranslationItem> _translations;
[DataMember]
public virtual ObservableCollection<TranslationItem> Translations
get
return _translations;
set
_translations = value;
foreach (TranslationItem ti in _translations)
ti.PropertyChanged += ti_PropertyChanged;
...
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
Translation t;
if(validationContext.ObjectInstance.GetType()== typeof(Translation))
t = validationContext.ObjectInstance as Translation;
else
t =( validationContext.ObjectInstance as TranslationItem).Translation;
if (!t.EnglishNotRequried)
if (!t.TranslationDict.ContainsKey(_englishLcid) || String.IsNullOrEmpty(t.TranslationDict[_englishLcid].Text))
yield return new ValidationResult("EnglishTranslationMissing");
我的迁移配置类中的 Seed 方法
protected override void Seed(DbContext context)
int deLcid = new CultureInfo("en").LCID;
int enLcid = new CultureInfo("en").LCID;
TranslationItem enStd = context.TranslationItems.FirstOrDefault(ti => ti.Text == "Standard" && ti.Lcid == enLcid)
?? new TranslationItem Text = "Standard", Lcid = enLcid ;
TranslationItem deStd = context.TranslationItems.FirstOrDefault(ti => ti.Text == "Standard" && ti.Lcid == deLcid)
?? new TranslationItem Text = "Standard", Lcid = deLcid ;
context.Translations.AddOrUpdate(
t => t.Translations,
new Translation
Translations = new ObservableCollection<TranslationItem>
enStd,
deStd
);
context.SaveChanges();
context.EventTypes.AddOrUpdate(
et => et.Name,
new EventType
Name = context.Translations.FirstOrDefault(t => t.Translations.Any(x => x.Lcid == enLcid && x.Text == "Standard") &&
t.Translations.Any(x => x.Lcid == deLcid && x.Text == "Standard") )
);
当我想更新数据库时出现以下错误
Unable to create a constant value of type 'PQS.Model.TranslationItem'. Only primitive types or enumeration types are supported in this context.
我不太确定如何解决这个问题。 我已经尝试先插入我的 TranslationItems,但是当我这样做时,验证失败,因为我猜需要翻译的 FK。
请帮忙!!
【问题讨论】:
【参考方案1】:我自己用一个小技巧修复了它(不完全是我喜欢的解决方案,但它现在可以工作)
我的问题:似乎AddOrUpdate(...)
我只能比较原始类型或枚举类型。
所以我添加了一个名为 SeedId
的附加 ID 来识别我的实体。
所以我在种子方法中的代码现在非常简单:
foreach (EventType eventtype in PqsDbContext.getStdEventTypes())
context.EventTypes.AddOrUpdate(et => et.SeedId, eventtype);
【讨论】:
以上是关于EF 迁移种子 - 无法创建“Application.Model.TranslationItem”类型的常量值的主要内容,如果未能解决你的问题,请参考以下文章
EF Code First Database Initializer和Migrations Confusion