【发布时间】:2012-12-28 03:17:17
【问题描述】:
我不断收到此错误消息:
异常详情:MySql.Data.MySqlClient.MySqlException:“字段列表”中的未知列“Extent1.RuleType”
我的映射:
public abstract class AlertRule
{
private DateTime? _updateDateTime = DateTime.Now;
[Key]
public int Id { get; set; }
public int TemplateId { get; set; }
public virtual AlertRuleTemplate Template { get; set; }
public string RuleType
{
get
{
if (Template == null)
return null;
return Template.Name;
}
}
}
public class AlertOutageRule:AlertRule
{
public virtual List<AlertRuleOutage> AlertRuleOutages { get; set; }
}
public class AlertMissingRule:AlertRule{}
public class AlertMetadataRule:AlertRule{}
public DbSet<AlertRule> AlertRules { get; set; }
modelBuilder.Entity<AlertRule>()
.Map<AlertOutageRule>(m => m.Requires("RuleType").HasValue("NewsOutage"))
.Map<AlertMetadataRule>(m => m.Requires("RuleType").HasValue("NewsMetadata"))
.Map<AlertMissingRule>(m => m.Requires("RuleType").HasValue("NewsMissing"));
//.Property(m => m.TemplateId).HasColumnType("int");
modelBuilder.Entity<AlertRule>().ToTable("AlertRule");
【问题讨论】:
-
RuleType 没有设置器,因此您无法将其映射到持久数据库列,这是 EF 在检索对象时重新实现正确类型所必需的。
-
@MortenMertner 你的意思是这个属性应该在数据库中有一个对应列吗?
-
是的;您必须添加 setter(以及列),或者删除映射中对该属性的所有引用。
标签: c# mysql entity-framework orm hierarchy