是否可以关联两个实体而不从实体框架 4.1 中的数据库中检索它们?
考虑以下场景:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Attach
就是针对这样的场景而设计的:它告诉 EF
ID
= 1 的Entity2
已存在于数据库中。Attach
之后实体处于Unchanged
状态,并且 EF 不会向该实体的数据库发送任何 UPDATE 或 INSERT 语句。仅更新entity1
中的外键。Attach
is made for such a scenario:It tells EF that
Entity2
withID
= 1 already exists in the database. The entity is inUnchanged
state afterAttach
and EF won't send any UPDATE or INSERT statement to the database for this entity. Just the foreign key inentity1
is updated.