一到实体框架6一对多的关系 [英] One to many relationship in Entity Framework 6

查看:216
本文介绍了一到实体框架6一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必须映射到列在现有的数据库中的两个简单的表:

I have two simple tables that have to map to columns in an existing database:

public class StockItem
{
    public System.Guid pkStockItemID { get; set; }

    public virtual List<StockItem_ExtendedProperties> ExtendedProperties { get; set; }
}

public class StockItem_ExtendedProperties
{
    public System.Guid pkStockItem_ExtendedPropertiesID { get; set; }

    public System.Guid fkStockItemId { get; set; }

    public virtual StockItem StockItem { get; set; }
}

以下是配置类为:

Following are the configuration classes for both:

public StockItemConfiguration ()
{
    this.HasMany(item => item.ExtendedProperties)
        .WithRequired(property => property.StockItem)
        .HasForeignKey(property => property.fkStockItemId)
        .WillCascadeOnDelete();
}

public StockItem_ExtendedPropertiesConfiguration ()
{
    this.HasRequired(property => property.StockItem)
        .WithMany(item => item.ExtendedProperties)
        .HasForeignKey(property => property.fkStockItemId)
        .WillCascadeOnDelete();
}

这导致错误:身份StockItem_ExtendedProperties该项目已经存在的元数据收集。参数名称:项目

似乎界定从表的两侧的关系导致错误。然而,可从两侧去除这个配置仍导致相同的错误。

It appears that defining the relationship from both sides of the table is causing the error. However, removing the configuration from either side still results in the same error.

在除了如何解决上述情况,我想知道是什么原因,哪些在这样的关系中的两个表中应配置。谢谢你。

In addition to how to fix the above, I'd like to know what the reason is and which of the two tables in such a relationship should be configured. Thanks.

推荐答案

原来,问题是在实体名称下划线 StockItem_ExtendedProperties 。去除摆脱了错误。使用EF 6.0.0.0这是通过的NuGet的最新版本。

Turns out the problem was the underscore in the entity name StockItem_ExtendedProperties. Removing that got rid of the error. Using EF 6.0.0.0 which was the latest available version via NuGet.

这篇关于一到实体框架6一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆