【问题标题】:Entity Framework code first Fluent mappingEntity Framework 代码优先 Fluent 映射
【发布时间】:2012-10-11 08:35:03
【问题描述】:

我有两张桌子

  • StoreOrderItem
  • 商店订单

现在 StoreOrder 有很多 StoreOrderItems 而 StoreOrderItem 有一个 StoreOrder(简单的一对多关系)

public class StoreOrderItemMap : EntityTypeConfiguration<StoreOrderItem>
{
        public StoreOrderItemMap()
        {
            this.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);
        }
}

public class StoreOrderMap : EntityTypeConfiguration<StoreOrder>
{
    public StoreOrderMap()
    {
        this.ToTable("StoreOrder");
        this.HasKey(op => op.Id);
        ....
    }
}

public class StoreOrderItem
{
    ....
    public virtual int StoreOrderId { get; set; }
    ....

    public virtual StoreOrder StoreOrder { get; set; }
}

public class StoreOrder
{
    .... 
    private ICollection<StoreOrderItem> _storeOrderItems;
    ....

    public virtual ICollection<StoreOrderItem> StoreOrderItems
    {
        get { return _storeOrderItems ?? (_storeOrderItems = new List<StoreOrderItem>()); }
        set { _storeOrderItems = value; }
    }

}

//The code which is used to add the configurations to the modelBuilder.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    System.Type configType = typeof(ContentMap);   //any of your configuration classes here
    var typesToRegister = Assembly.GetAssembly(configType).GetTypes()
        .Where(type => !String.IsNullOrEmpty(type.Namespace))
        .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
        foreach (var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);
            modelBuilder.Configurations.Add(configurationInstance);
        }
        base.OnModelCreating(modelBuilder);
}

注意我在现有数据库上运行此代码,我如何告诉 EF 不要选择“StoreOrder_Id”而使用现有列“order_id”?

这是错误:

“/”应用程序中的服务器错误。

列名“StoreOrder_Id”无效。

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

异常详细信息:System.Data.SqlClient.SqlException:无效列 名称“StoreOrder_Id”。

【问题讨论】:

  • 你确定你的地图是用于制图的吗?
  • 对不起,我不明白你的意思......
  • 请问您在构建模型时是否已将StoreOrderItemMap 添加到DbModelBuilderConfigurations 集合中?
  • 在您的 DbContext 类中是否有方法保护覆盖 void OnModelCreating(DbModelBuilder modelBuilder)?
  • 是的,这是使用反射为每个 typeof(EntityTypeConfiguration) 完成的

标签: c# entity-framework


【解决方案1】:

折腾了几天才发现问题......

StoreOrder 类中有另一个属性

public virtual IList<StoreOrderItem> NonVoucherOrderItems
{
    get
    {
        return this.StoreOrderItems.Where(x => !x.IsVoucher()).ToList();
    }
}

这个评价太早了,搞得乱七八糟,改成

public virtual IEnumerable<StoreOrderItem> NonVoucherOrderItems
{
    get
    {
        return this.StoreOrderItems.Where(x => !x.IsVoucher());
    }
}

它立即起作用了!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    相关资源
    最近更新 更多