一到实体框架6一对多的关系一到、实体、框架、关系

由网友(竖起中指操起生活)分享简介:我有必须映射到列在现有的数据库中的两个简单的表:I have two simple tables that have to map to columns in an existing database:public class StockItem{public System.Guid pkStockItemID...

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

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.

阅读全文

相关推荐

最新文章