Entity Framework 4.1 RTW Code First - POCO 一对多是否需要引用子实体和子实体主键?

发布于 2024-11-02 09:12:20 字数 1106 浏览 0 评论 0原文

在一种情况下,您的父类有一个子类,映射实体的最佳模式是什么。

我见过很多建议,其中父级同时引用了子类和子类 id。例如

public class Parent
{
    public int Id
    {
        get;
        set;
    }

    public int ChildId
    {
        get;
        set;
    }

    public virtual Child Child
    {
        get;
        set;
    }
}

public class Child
{
    public int Id
    {
        get;
        set;
    }
}

public class ParentMapping : EntityTypeConfiguration<Parent>
{
    public ParentMapping()
    {
        HasKey(x => x.Id);

        HasRequired(X => x.Child)
            .WithMany()
            .Map(x => x.ToTable("Parent")
                        .MapKey("ChildId"));
    }
}

,使用这种模式,在保存父级时,如果您想将子级替换为不同的但现有子级,我看到的示例只是更新 ChildId 而不是 Child,这感觉不对,因为对象与自身不同步。

如果没有 ChildId,代码看起来会更整洁,但是使用这种模式,我无法使用现有的子项保存父项,因为 EF 正在尝试保存新的子项。

public class Parent
{
    public int Id
    {
        get;
        set;
    }

    public virtual Child Child
    {
        get;
        set;
    }
}

最好的模式是什么,我想知道是否需要 ChildId,那么 Child 属性如何保持同步以及是否会从数据库延迟加载。

In a situation where you have a parent class which has one child class, what is the best pattern for mapping the entities.

I have seen a lot of suggestions where the parent has a reference to both the child class, and the child class id. E.g.

public class Parent
{
    public int Id
    {
        get;
        set;
    }

    public int ChildId
    {
        get;
        set;
    }

    public virtual Child Child
    {
        get;
        set;
    }
}

public class Child
{
    public int Id
    {
        get;
        set;
    }
}

public class ParentMapping : EntityTypeConfiguration<Parent>
{
    public ParentMapping()
    {
        HasKey(x => x.Id);

        HasRequired(X => x.Child)
            .WithMany()
            .Map(x => x.ToTable("Parent")
                        .MapKey("ChildId"));
    }
}

With this pattern, when saving the parent, if you want to swap out the child for a different but existing child, examples I have seen just update the ChildId and not the Child which feels wrong because the object is out of sync with itself.

The code looks neater without the ChildId but with this pattern I am having trouble saving the parent using an existing child because EF is trying to save a new child.

public class Parent
{
    public int Id
    {
        get;
        set;
    }

    public virtual Child Child
    {
        get;
        set;
    }
}

What is the best pattern, I would like to know if the ChildId is needed, then how is the Child property kept in sync and will it be lazy loaded from the database or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-11-09 09:12:21

这是外键和独立关联之间的区别。使用外键关联时,您实际上可以只使用键而不加载相关对象。如果您加载了引用,它会使引用不同步 - 但情况并非总是如此。如果您想保持引用同步,您几乎又回到了必须通过独立关联来解决的情况。

如果您公开外键,您应该使用它,因为它使很多事情变得更加容易。如果你使用独立关联,你应该做类似的事情:

var parent = GetUpdatedParentSomehow();
// Dummy object for the old child if the relation is not loaded
parent.Child = new Child { Id = oldChildId }; 
// Attach the parent
context.Parents.Attach(parent);

// Create dummy for new child (you can also load a child from DB)
var child = new Child { ID = newChildId };
// No attach the child to the context so the context
// doesn't track it as a new child
context.Childs.Attach(child);
// Set a new child
parent.Child = child;
// Set parent as modified
context.Entry(parent).State = EntityState.Modified;
context.SaveChanges();

有一个非常奇怪的部分,我正在为老孩子创建虚拟人。我几乎可以肯定,如果我在附加父级并设置新子级之前不这样做,我会在保存更改期间遇到一些异常(在独立关联的情况下)。

This is difference between foreign key and independent association. When using foreign key association you can really use just key without loading the related object. It makes the reference out of sync if you have it loaded - which is not always the case. If you want to keep the reference in sync you are almost back in the situation which you must solve with independent association.

If you expose foreign key you should use it because it makes a lot of things much more easier. If you use the independent association you should do something like:

var parent = GetUpdatedParentSomehow();
// Dummy object for the old child if the relation is not loaded
parent.Child = new Child { Id = oldChildId }; 
// Attach the parent
context.Parents.Attach(parent);

// Create dummy for new child (you can also load a child from DB)
var child = new Child { ID = newChildId };
// No attach the child to the context so the context
// doesn't track it as a new child
context.Childs.Attach(child);
// Set a new child
parent.Child = child;
// Set parent as modified
context.Entry(parent).State = EntityState.Modified;
context.SaveChanges();

There is very strange part where I'm creating dummy for the old child. I'm almost sure that if I don't do it before attaching the parent and setting the new child I will get some exception during saving changes (in case of independent association).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文