简体   繁体   English

实体框架代码优先Fluent映射

[英]Entity Framework code first Fluent mapping

I have two tables 我有两张桌子

  • StoreOrderItem StoreOrderItem
  • StoreOrder StoreOrder

Now StoreOrder has many StoreOrderItems and StoreOrderItem has one StoreOrder (simple 1 to many relationship) 现在,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);
}

Note I am running this code on an existing DB, how do I tell EF to not select "StoreOrder_Id" and to instead use the existing column "order_id"? 注意我正在现有数据库上运行此代码,如何告诉EF不要选择“ StoreOrder_Id”,而要使用现有列“ order_id”?

Here is the error: 这是错误:

Server Error in '/' Application. “ /”应用程序中的服务器错误。

Invalid column name 'StoreOrder_Id'. 无效的列名“ StoreOrder_Id”。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'StoreOrder_Id'. 异常详细信息:System.Data.SqlClient.SqlException:无效的列名“ StoreOrder_Id”。

Found the problem after a few days of mind bending ...... 几天弯腰后才发现问题......

There was another property in the StoreOrder class StoreOrder类中还有另一个属性

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

This was being evaluated too early and causing all sorts of chaos, changed to 对此评估还为时过早,并造成了各种混乱,更改为

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

And it worked immediately!! 它立即起作用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM