C# 实体框架代码第一流映射

C# 实体框架代码第一流映射,c#,entity-framework,C#,Entity Framework,我有两张桌子 StoreOrderItem 商店订单 现在StoreOrder有许多StoreOrderItems,StoreOrderItem有一个StoreOrder(简单的1对多关系) 公共类StoreOrderItemMap:EntityTypeConfiguration { public StoreOrderItemMap() { 本表为ToTable(“StoreOrderItem”); this.HasKey(op=>op.Id); this.Property(op=>op.S

我有两张桌子

  • StoreOrderItem
  • 商店订单
现在StoreOrder有许多StoreOrderItems,StoreOrderItem有一个StoreOrder(简单的1对多关系)

公共类StoreOrderItemMap:EntityTypeConfiguration { public StoreOrderItemMap() { 本表为ToTable(“StoreOrderItem”); this.HasKey(op=>op.Id); this.Property(op=>op.StoreOrderId).HasColumnName(“order_id”); ... this.HasRequired(so=>so.StoreOrder) .WithMany(soi=>soi.StoreOrderItems) .HasForeignKey(so=>so.StoreOrderId); } } 公共类StoreOrderMap:EntityTypeConfiguration { public StoreOrderMap() { 本表为ToTable(“StoreOrder”); this.HasKey(op=>op.Id); .... } } 公共类StoreOrderItem { .... 公共虚拟int StoreOrderId{get;set;} .... 公共虚拟StoreOrder StoreOrder{get;set;} } 公共类存储顺序 { .... 私有ICollection\u storeOrderItems; .... 公共虚拟ICollection StoreOrderItems { 获取{return _storeOrderItems???(_storeOrderItems=new List());} 设置{u storeOrderItems=value;} } } //用于将配置添加到modelBuilder的代码。 模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder) { System.Type configType=typeof(ContentMap);//此处的任何配置类 var typesToRegister=Assembly.GetAssembly(configType.GetTypes()) .Where(type=>!String.IsNullOrEmpty(type.Namespace)) 其中(type=>type.BaseType!=null&&type.BaseType.IsGenericType&&type.BaseType.GetGenericTypeDefinition()==typeof(EntityTypeConfiguration)); foreach(TypeStoreRegister中的变量类型) { dynamic configurationInstance=Activator.CreateInstance(类型); modelBuilder.Configurations.Add(configurationInstance); } 基于模型创建(modelBuilder); } 注意:我在现有数据库上运行此代码,如何告诉EF不要选择“StoreOrder\u Id”,而是使用现有列“order\u Id”

以下是错误:

“/”应用程序中出现服务器错误

列名“StoreOrder\u Id”无效

描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源

异常详细信息:System.Data.SqlClient.SqlException:无效列 名称“StoreOrder\u Id”


经过几天的苦思冥想,终于发现了问题

StoreOrder类中还有另一个属性

public virtual IList<StoreOrderItem> NonVoucherOrderItems
{
    get
    {
        return this.StoreOrderItems.Where(x => !x.IsVoucher()).ToList();
    }
}
公共虚拟IList非VoucherOrderItems
{
得到
{
返回此.StoreOrderItems.Where(x=>!x.is凭单()).ToList();
}
}
这被评估得太早,造成了各种混乱,改为

public virtual IEnumerable<StoreOrderItem> NonVoucherOrderItems
{
    get
    {
        return this.StoreOrderItems.Where(x => !x.IsVoucher());
    }
}
公共虚拟IEnumerable非VoucherOrderItems
{
得到
{
返回此.StoreOrderItems.Where(x=>!x.IsVoucher());
}
}

而且它立刻起作用了

经过几天的思考后发现了问题

StoreOrder类中还有另一个属性

public virtual IList<StoreOrderItem> NonVoucherOrderItems
{
    get
    {
        return this.StoreOrderItems.Where(x => !x.IsVoucher()).ToList();
    }
}
公共虚拟IList非VoucherOrderItems
{
得到
{
返回此.StoreOrderItems.Where(x=>!x.is凭单()).ToList();
}
}
这被评估得太早,造成了各种混乱,改为

public virtual IEnumerable<StoreOrderItem> NonVoucherOrderItems
{
    get
    {
        return this.StoreOrderItems.Where(x => !x.IsVoucher());
    }
}
公共虚拟IEnumerable非VoucherOrderItems
{
得到
{
返回此.StoreOrderItems.Where(x=>!x.IsVoucher());
}
}

而且它立刻起作用了

你确定你的映射用于映射吗?很抱歉,我不明白你的意思…我问你在构建模型时是否已将
StoreOrderItemMap
添加到
DbModelBuilder
Configurations
集合中?在DbContext类中,你在模型创建时是否有方法保护覆盖void(DbModelBuilder modelBuilder)?是的,这是针对每种类型的(EntityTypeConfiguration)执行的使用Reflection你确定你的映射用于映射吗?很抱歉,我不明白你的意思…我问你是否在构建模型时将
StoreOrderItemMap
添加到
DbModelBuilder
配置
集合中?在DbContext类中,你有方法保护覆盖void OnModelCreating吗(DbModelBuilder modelBuilder)?是的,这是使用反射对每种类型的(EntityTypeConfiguration)执行的