实体框架附加 [英] Entity Framework Attach
问题描述
我很确定,这个问题的答案应该很简单,但是经过数小时的谷歌搜索后我找不到答案...
- 我确实使用实体框架有两种对象,比如说一个街道和几个房子。这种关系很清楚。 One Street包含一个或多个房屋。
EF中的此对象如下所示:
ctx.street .houses
等
如果我使用
ctx.street.houses。添加(新房)
我最终在数据库中使用了新的房屋对象。很好。
但是如何在街上添加一个已经给出的房子物品呢?我总是得到一个新的房子对象,无法使用我已经创建的对象。
我找到了Attach()函数而不是Add()函数对于集合,但
ctx.street.houses.Attach()
函数不存在于我的环境,所以这根本不是解决方案。
任何人都可以解决这个问题吗?
public partial class DBEntity:DbContext
{
public DBEntity()
: base ( name = DBEntity1)
{
}
protected 覆盖 void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet< client>客户{{span class =code-keyword>获取; set ; }
public virtual DbSet< contactperson> ContactPersons { get ; set ;}
}
客户类:
public partial class 客户端
{
[System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Usage, CA2214:DoNotCallOverridableMethodsInConstructors)]
public 客户端()
{
this .ContactPersons = new HashSet< contactperson>();
}
public int Id {获得跨度>; set ; }
public string 公司{ get 跨度>; set ; }
public System.DateTime Date_Creation { get ; set ; }
public bool 有效{获取跨度>; set ; }
[System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Usage, CA2227:CollectionPropertiesShouldBeReadOnly)]
public virtual ICollection< contactperson> ContactPersons { get ; set ; }
[System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Usage, CA2227:CollectionPropertiesShouldBeReadOnly)]
}
和ContactPerson课程:
public partial class ContactPerson
{
[System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Usage, CA2214:DoNotCallOverridableMethodsInConstructors)]
public ContactPerson()
{
}
public int Id { get ; set ; }
public string FirstName { get 跨度>; set ; }
public string LastName { get 跨度>; set ; }
public System.DateTime Date_Creation { get ; set ; }
public bool 有效{获取跨度>; set ; }
public string 电子邮件{ get 跨度>; set ; }
public string Phone_Office { get 跨度>; set ; }
public string Phone_Mobile { get 跨度>; set ; }
public virtual 客户端客户端{获得跨度>; set ; }
[System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Usage, CA2227:CollectionPropertiesShouldBeReadOnly)]
}
[/由Maciej Los编辑(内容从评论/回答中移出)
假设您在众议院拥有街道导航属性实体,您应该可以这样做:
existingHouse.Street = existingStreet;
< blockquote>首先,请阅读我对这个问题的评论。
房子和街道之间的关系是明确的:1条街道可以包含几栋房屋。被称为一对多。
根据我的理解,层次结构的模型如下:
Street
+ - --Houses
+ ---- House
+ ---- House
+ ---- House
所以,我不明白你的意思是说 将已经给予的房子附加到街道,因为每个房子都属于属于单一街道的房屋集合。
I am pretty sure, the answer to this question should be easy, but I cannot find the answer after hours of googling...
- I do use Entity Framework and have two kind of objects, lets say one Street and several houses. The relationship is clear. One Street contains one or more houses.
This objects in EF looks like the following:
ctx.street.houses
etc.
If I use
ctx.street.houses.Add(newhouse)
I end up with a new house object in a database. Fine.
But how can I add an already given "house" object into the street? I always get a new house object and cannot get it to work to use my already created objects.
I found the Attach() function instead of the Add() function for the collection, but the
ctx.street.houses.Attach()
Function doesn`t exist in my environment, so this isn`t a solution at all.
Any one who can clear this up?
[EDIT]
public partial class DBEntity: DbContext
{
public DBEntity()
: base("name=DBEntity1")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<client> Clients { get; set; }
public virtual DbSet<contactperson> ContactPersons { get; set;}
}
The Clients class:
public partial class Client
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Client()
{
this.ContactPersons = new HashSet<contactperson>();
}
public int Id { get; set; }
public string Company { get; set; }
public System.DateTime Date_Creation { get; set; }
public bool Active { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<contactperson> ContactPersons { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
}
and the ContactPerson class:
public partial class ContactPerson
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ContactPerson()
{
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public System.DateTime Date_Creation { get; set; }
public bool Active { get; set; }
public string EMail { get; set; }
public string Phone_Office { get; set; }
public string Phone_Mobile { get; set; }
public virtual Client Client { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
}
[/EDIT by Maciej Los (content moved from comment/answer]
Assuming you have a "Street" navigation property on the House entity, you should be able to do something like this:
existingHouse.Street = existingStreet;
First of all, please, read my comment to the question.
The relationship between house and street is preety clear: 1 street can contain several houses. This relationship is called one to many.
As per my understanding, the model of hierarchy looks like:
Street +----Houses +----House +----House +----House
So, i do not understand what you mean by saying "attach already given house into the street", because each house belongs to the collection of houses which belong to the single street.
这篇关于实体框架附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!