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/2/visual-studio-2010/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
Entity framework EF Code First和modelbuilder-如何使用相同的ID建立一对一的关系?_Entity Framework_Foreign Keys_Entity Relationship_One To One - Fatal编程技术网

Entity framework EF Code First和modelbuilder-如何使用相同的ID建立一对一的关系?

Entity framework EF Code First和modelbuilder-如何使用相同的ID建立一对一的关系?,entity-framework,foreign-keys,entity-relationship,one-to-one,Entity Framework,Foreign Keys,Entity Relationship,One To One,我有两门课: public class Account { public int Id {get;set;} public PayoffData PayoffData {get;set;} } public class PayoffData { public int Id {get;set;} //some other fields } 现在,我希望classPayoffData拥有与classAccount相同的id,并且它需要一对一的关系(Account可以(或

我有两门课:

public class Account
{
   public int Id {get;set;}

   public PayoffData PayoffData {get;set;}
}

public class PayoffData
{
   public int Id {get;set;}
   //some other fields
}
现在,我希望class
PayoffData
拥有与class
Account
相同的id,并且它需要一对一的关系(Account可以(或不能)拥有一个PayoffData)

我曾尝试使用modelbuilder做过一些事情,但据我所知,要在
PayoffData
类中设置foreignkey,我需要设置
。有许多关系,这是我不想要的。如何使用modelbuilder解决问题?(我不想使用数据注释方法)

modelBuilder.Entity()
.has可选(a=>a.PayoffData)
.WithRequired();
两个表都有一个名为
Id
的主键列,
PayoffData
表中的主键
Id
将同时是
账户
表的外键

modelBuilder.Entity<Account>()
    .HasOptional(a => a.PayoffData)
    .WithRequired();