Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用实体框架插入引用其他表中记录的记录_C#_Entity Framework_C# 4.0_Entity Framework 4 - Fatal编程技术网

C# 使用实体框架插入引用其他表中记录的记录

C# 使用实体框架插入引用其他表中记录的记录,c#,entity-framework,c#-4.0,entity-framework-4,C#,Entity Framework,C# 4.0,Entity Framework 4,这是一个相当基本的问题,但我很惊讶没有找到一个像样的答案 考虑以下数据库架构: Person {PersionID, Name, CountryOfBirthID [references Country]} Country {CountryID, CountryName} 应用程序在启动期间从数据库中读取所有国家,并将其保存在列表中。 每当我添加一个新人时,我都会用上面提到的列表中的ID和数据创建一个新的Country对象。我希望EF插入一个ID引用Country表中的记录的新人,但它将一条新

这是一个相当基本的问题,但我很惊讶没有找到一个像样的答案

考虑以下数据库架构:

Person {PersionID, Name, CountryOfBirthID [references Country]}
Country {CountryID, CountryName}
应用程序在启动期间从数据库中读取所有国家,并将其保存在列表中。 每当我添加一个新人时,我都会用上面提到的列表中的ID和数据创建一个新的Country对象。我希望EF插入一个ID引用Country表中的记录的新人,但它将一条新记录插入Country表并为其分配一个新ID,这显然不是我想要的

插入引用其他表中记录的新记录的常规方法是什么?
我假设,每当我添加Person(在相同的上下文下)时,我都可以通过ID获取国家记录并重用获取的记录,但是是否有其他方法允许我只访问Person表?

简单的答案是,您需要先
'Attach'
您的
国家(即,让实体Framwork知道它已经存在于上下文/数据库中):

此外,如果在Visual Studio中使用设计器创建实体,则实体应包含名为
“EntityReference”
的属性,您可以使用该属性实现相同的结果:

var person = new Person {};
person.Country.EntityReference = new EntityKey("MyEntities.Countries", "CountryId", countryId);
(用适当的值替换“MyEntities.Countries”和“CountryId”)

但是,我认为最简单的方法是“公开”实体中的外键:

class Person
{
  ....
  Country CountryOfBirth { get; set; }
  int CountryOfBirthID { get; set; } 
}

//then:

var person = new Person { ... };
person.CountryOfBirthId = 2;
如果您正在寻找一种更通用的方法来处理此问题,请查找
标识映射
模式

class Person
{
  ....
  Country CountryOfBirth { get; set; }
  int CountryOfBirthID { get; set; } 
}

//then:

var person = new Person { ... };
person.CountryOfBirthId = 2;