首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphDiff和EF6.1 -递归的多到多关系

GraphDiff和EF6.1 -递归的多到多关系
EN

Stack Overflow用户
提问于 2014-09-14 16:19:56
回答 1查看 339关注 0票数 2

对于GraphDiff附加一个具有递归的多到多关系的对象,我有一个问题。我使用的是GraphDiff 2.0.1和实体框架6.1.1。

该方案管理的累进,可以有0-n RecipeLines,其中包含一个链接到另一个食谱,即ComponentRecipe,这有RecipeLines等。

我将一个食谱对象传递到存储库中,存储库中有1条菜谱行,其中菜谱对象指向父库,ComponentRecipe对象指向不同的菜谱。

当GraphDiff附加项目时,它返回的attachedItem有2条菜谱行,这两行都是相同的,并让ComponentRecipe指向与菜谱相同的对象,从而在自己上创建一个循环。

我不确定这是否是GraphDiff的问题,或者更有可能我的EF映射或GraphDiff映射有问题。任何帮助感谢,让我知道如果你需要更多的信息,我可以提供一个简单的程序来演示这个问题。

域模型

代码语言:javascript
复制
public class Recipe : EntityBase
{
        public override object Key
        {
            get { return Id; }
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public virtual IList<RecipeLine> RecipeLines { get; set; }

 }

public class RecipeLine : EntityBase
{
        public override object Key
        {
            get { return Id; }
        }

        public int Id { get; set; }

        public int RecipeId { get; set; }

        public int ComponentRecipeId { get; set; }

        public decimal Quantity { get; set; }

        public virtual Recipe Recipe { get; set; }

        public virtual Recipe ComponentRecipe { get; set; }
}

数据上下文

代码语言:javascript
复制
class DataContext : DbContext
    {
        public DataContext()
            : base("Connection")
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
        }

        public DbSet<Recipe> Recipes { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<DataContext>(null);

            modelBuilder.Entity<Recipe>().ToTable("Recipes");
            modelBuilder.Entity<Recipe>().HasKey(x => x.Id);
            modelBuilder.Entity<Recipe>().Property(x => x.Id).HasColumnName("Id");
            modelBuilder.Entity<Recipe>().Property(x => x.Name).HasColumnName("Name");

            modelBuilder.Entity<RecipeLine>().ToTable("RecipeLines");
            modelBuilder.Entity<RecipeLine>().HasKey(x => x.Id);
            modelBuilder.Entity<RecipeLine>().Property(x => x.Id).HasColumnName("Id");
            modelBuilder.Entity<RecipeLine>().Property(x => x.RecipeId).HasColumnName("RecipeId");
            modelBuilder.Entity<RecipeLine>().Property(x => x.ComponentRecipeId).HasColumnName("ComponentRecipeId");
            modelBuilder.Entity<RecipeLine>().Property(x => x.Quantity).HasColumnName("Quantity").HasPrecision(18, 5);

            modelBuilder.Entity<Recipe>().HasMany(x => x.RecipeLines).WithRequired(x => x.Recipe).HasForeignKey(x => x.RecipeId);

            modelBuilder.Entity<RecipeLine>().HasRequired(x => x.ComponentRecipe).WithMany().HasForeignKey(x => x.ComponentRecipeId);

        }
    }

仓库

代码语言:javascript
复制
public override void UpdateEntity(IEntity entity)
{
            using (var context = new DataContext())
            {
                context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
                var item = (Recipe)entity;
                var attachedItem = context.UpdateGraph(item,
                    a => a.OwnedCollection(b => b.RecipeLines, c => c.AssociatedEntity(d => d.ComponentRecipe)));
                context.SaveChanges();
            }
}

附加上的数据上下文日志

代码语言:javascript
复制
Opened connection at 14/09/2014 16:57:06 +01:00

SELECT 
    [Project1].[Id] AS [Id], 
    [Project1].[Name] AS [Name], 
    [Project1].[C1] AS [C1], 
    [Project1].[Id1] AS [Id1], 
    [Project1].[RecipeId] AS [RecipeId], 
    [Project1].[ComponentRecipeId] AS [ComponentRecipeId], 
    [Project1].[Quantity] AS [Quantity], 
    [Project1].[Id2] AS [Id2], 
    [Project1].[Name1] AS [Name1]
    FROM ( SELECT 
        [Limit1].[Id] AS [Id], 
        [Limit1].[Name] AS [Name], 
        [Join1].[Id1] AS [Id1], 
        [Join1].[RecipeId] AS [RecipeId], 
        [Join1].[ComponentRecipeId] AS [ComponentRecipeId], 
        [Join1].[Quantity] AS [Quantity], 
        [Join1].[Id2] AS [Id2], 
        [Join1].[Name] AS [Name1], 
        CASE WHEN ([Join1].[Id1] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
        FROM   (SELECT TOP (2) [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name]
            FROM [dbo].[Recipes] AS [Extent1]
            WHERE 11 = [Extent1].[Id] ) AS [Limit1]
        LEFT OUTER JOIN  (SELECT [Extent2].[Id] AS [Id1], [Extent2].[RecipeId] AS [RecipeId], [Extent2].[ComponentRecipeId] AS [ComponentRecipeId], [Extent2].[Quantity] AS [Quantity], [Extent3].[Id] AS [Id2], [Extent3].[Name] AS [Name]
            FROM  [dbo].[RecipeLines] AS [Extent2]
            INNER JOIN [dbo].[Recipes] AS [Extent3] ON [Extent2].[ComponentRecipeId] = [Extent3].[Id] ) AS [Join1] ON [Limit1].[Id] = [Join1].[RecipeId]
    )  AS [Project1]
    ORDER BY [Project1].[Id] ASC, [Project1].[C1] ASC


-- Executing at 14/09/2014 16:57:06 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 14/09/2014 16:57:06 +01:00

Opened connection at 14/09/2014 16:57:07 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name]
    FROM [dbo].[Recipes] AS [Extent1]
    WHERE [Extent1].[Id] = 10


-- Executing at 14/09/2014 16:57:07 +01:00

-- Completed in 0 ms with result: SqlDataReader



Closed connection at 14/09/2014 16:57:07 +01:00

基于 SaveChanges的数据上下文日志

代码语言:javascript
复制
Opened connection at 14/09/2014 16:58:45 +01:00

INSERT [dbo].[RecipeLines]([RecipeId], [ComponentRecipeId], [Quantity])
VALUES (@0, @1, @2)
SELECT [Id]
FROM [dbo].[RecipeLines]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()


-- @0: '11' (Type = Int32)

-- @1: '11' (Type = Int32)

-- @2: '1' (Type = Decimal, Precision = 18, Scale = 5)

-- Executing at 14/09/2014 16:58:46 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 14/09/2014 16:58:46 +01:00
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-26 17:31:10

这是GraphDiff中的一个bug。附加父导航属性的代码尚未准备好处理相同类型的多个父级。我刚刚用拉动请求#102拉动请求#102的分支上修正了这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25835344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档