简体   繁体   English

将具有导航属性的对象添加到Db时会发生什么情况?

[英]What is happening while adding object with navigation properies to Db?

I've got class like that: 我有这样的课:

class City
{
    long CityId {get;set;} 
    string Name {get;set;} 
    List<House> Houses {get;set;} 

}

with filled List<House> with some Houses objects and I want to add City to db with db.Cities.Add(someCity) . 用一些Houses对象填充List<House> ,我想使用db.Cities.Add(someCity)City添加到db中。

Is Houses also will be added automatically in that moment? 难道Houses也会在那一刻自动添加吗?

What I need to do to add also Houses automatically, assuming that my Navigation Properties are set up properly? 假设我的导航属性设置正确,我还需要做些什么来自动添加Houses

Yes, they will, but take into account that if you do it this way, new House entries will be created in the table. 是的,他们会的,但是要考虑到,如果您采用这种方式,则会在表中创建新的House条目。 If you want to associate existing houses you will need to add an entity retrieved from database. 如果要关联现有房屋,则需要添加从数据库检索的实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM