是否可以关联两个实体而不从实体框架 4.1 中的数据库中检索它们?

发布于 2024-12-01 14:16:36 字数 1610 浏览 2 评论 0原文

考虑以下场景:

public class Entity1
{
    virtual public Int32 ID { get; set; }
    [Required]
    virtual public String Name { get; set; }
    [Required]
    virtual public Entity2 ReferenceToEntity2 { get; set; }
}

public class Entity2
{
    virtual public Int32 ID { get; set; }
    [Required]
    virtual public String Name { get; set; }
    virtual public IList<Entity1> ListOfEntity1 { get; set; }
}

[TestClass]
public class EntityFrameworkTests
{
    [TestMethod]
    public void Should_save_entity_with_reference_to_entity_with_required_fields()
    {
        using (var db = new MyContext())
        {
            var entity1 = new Entity1
            {
                Name = "My name",
                ReferenceToEntity2 = new Entity2 { ID = 1 } // reference (Entity2 with ID=1) already exists in the database 
            };
            db.Entity1List.Add(entity1);
            db.SaveChanges(); // exception here
        }
    }

    [TestMethod]
    [ExpectedException(typeof(DbEntityValidationException))]
    public void Should_not_save_empty_entity2_name()
    {
        using (var db = new MyContext())
        {
            var entity2 = new Entity2 { Name = "" };
            db.Entity2List.Add(entity2);
            db.SaveChanges();
        }
    }
}

第一个测试未通过。当尝试保存 Entity1 实例时,引用 Entity2 的 Name 属性,引发 DbEntityValidationException。

例如,我知道我可以通过 ID 加载Entity2实例并使用entity1.ReferenceToEntity2 = db.Entity2List.Find(entity2Id)关联它。但我实际上有一个包含大量引用的实体,我不想只为了关联一个外键而访问数据库 10 次!

是否可以将新的实体 1 实例插入到与现有实体 2 记录相关的数据库中,而无需前往数据库检索实体 2?

提前致谢!

Consider the following scenario:

public class Entity1
{
    virtual public Int32 ID { get; set; }
    [Required]
    virtual public String Name { get; set; }
    [Required]
    virtual public Entity2 ReferenceToEntity2 { get; set; }
}

public class Entity2
{
    virtual public Int32 ID { get; set; }
    [Required]
    virtual public String Name { get; set; }
    virtual public IList<Entity1> ListOfEntity1 { get; set; }
}

[TestClass]
public class EntityFrameworkTests
{
    [TestMethod]
    public void Should_save_entity_with_reference_to_entity_with_required_fields()
    {
        using (var db = new MyContext())
        {
            var entity1 = new Entity1
            {
                Name = "My name",
                ReferenceToEntity2 = new Entity2 { ID = 1 } // reference (Entity2 with ID=1) already exists in the database 
            };
            db.Entity1List.Add(entity1);
            db.SaveChanges(); // exception here
        }
    }

    [TestMethod]
    [ExpectedException(typeof(DbEntityValidationException))]
    public void Should_not_save_empty_entity2_name()
    {
        using (var db = new MyContext())
        {
            var entity2 = new Entity2 { Name = "" };
            db.Entity2List.Add(entity2);
            db.SaveChanges();
        }
    }
}

The first test is not passing. A DbEntityValidationException is raised, referring to the Name property of Entity2 when trying to save Entity1 instance.

I know that I could load entity2 instance by ID and associate it using entity1.ReferenceToEntity2 = db.Entity2List.Find(entity2Id), for example. But I actually have a entity with lots of references, and I don't want to go to database 10 times only to associate a foreign key!

Is it possible to insert a new entity1 instance into the database relating to an existing entity2 record without having to go to database to retrieve entity2?

Thanks in advance!

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

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

发布评论

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

评论(1

骷髅 2024-12-08 14:16:36

Attach 就是针对这样的场景而设计的:

using (var db = new MyContext())
{
    var entity1 = new Entity1
    {
        Name = "My name",
        ReferenceToEntity2 = new Entity2 { ID = 1 }
        // reference (Entity2 with ID=1) already exists in the database 
    };
    db.Entity2List.Attach(entity1.ReferenceToEntity2); // must be BEFORE Add
    db.Entity1List.Add(entity1);
    db.SaveChanges();
}

它告诉 EF ID = 1 的 Entity2 已存在于数据库中。 Attach 之后实体处于 Unchanged 状态,并且 EF 不会向该实体的数据库发送任何 UPDATE 或 INSERT 语句。仅更新 entity1 中的外键。

Attach is made for such a scenario:

using (var db = new MyContext())
{
    var entity1 = new Entity1
    {
        Name = "My name",
        ReferenceToEntity2 = new Entity2 { ID = 1 }
        // reference (Entity2 with ID=1) already exists in the database 
    };
    db.Entity2List.Attach(entity1.ReferenceToEntity2); // must be BEFORE Add
    db.Entity1List.Add(entity1);
    db.SaveChanges();
}

It tells EF that Entity2 with ID = 1 already exists in the database. The entity is in Unchanged state after Attach and EF won't send any UPDATE or INSERT statement to the database for this entity. Just the foreign key in entity1 is updated.

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