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
C# 如何使用主键和从属对象的外键_C#_Entity Framework_Foreign Keys_Foreign Key Relationship - Fatal编程技术网

C# 如何使用主键和从属对象的外键

C# 如何使用主键和从属对象的外键,c#,entity-framework,foreign-keys,foreign-key-relationship,C#,Entity Framework,Foreign Keys,Foreign Key Relationship,我在数据库中有两个对象,它们之间的关系如下。我按照说明创建一个新联系人和帐户,这分别是一个新的主体和从属对象。下面是我的代码 当创建newContact和newAccount时,这种方式可以很好地工作。业务联系人uid和业务地址uid显示新联系人的uid。但是,newContact的帮助是空的。你看到我的答案了吗?我能得到反馈吗?@PaoloCosta我试过了,但没用。我想我的问题是双向导航,而不是一对多的关系。 using (var frontendDb = new salon_databas

我在数据库中有两个对象,它们之间的关系如下。我按照说明创建一个新联系人和帐户,这分别是一个新的主体和从属对象。下面是我的代码


当创建newContact和newAccount时,这种方式可以很好地工作。业务联系人uid和业务地址uid显示新联系人的uid。但是,newContact的帮助是空的。你看到我的答案了吗?我能得到反馈吗?@PaoloCosta我试过了,但没用。我想我的问题是双向导航,而不是一对多的关系。
using (var frontendDb = new salon_database_test())
{
     var newContact = new Contact
     {
         first_name = model.FirstName,
         last_name = model.LastName,
         email = model.Email,
     };
     frontendDb.contacts.Add(newContact);

     var newAccount = new Account
     {
         name = model.BusinessName,
         country_id = model.Country,
         BusinessContact = newContact,
         BusinessAddress = newContact
     };
     frontendDb.accounts.Add(newAccount);

     frontendDb.SaveChanges();
}
public class Contact
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity), Column(Order=1)]
    public int uid { get; set; }

    [Column(Order = 2)]
    public int? aid { get; set; }
    [IgnoreDataMember, ForeignKey("aid")]
    public virtual Account account_owner { get; set; }

    //..others..
}

public class Account
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity), Column(Order=1)]
    public int uid { get; set; }

    // Address of the business 
    public int? business_address_uid { get; set; }
    [IgnoreDataMember, ForeignKey("business_address_uid")]
    public virtual Contact BusinessAddress { get; set; }

    // Contact details for the main contact of the account (business owner)
    public int? business_contact_uid { get; set; }
    [IgnoreDataMember, ForeignKey("business_contact_uid")]
    public virtual Contact BusinessContact { get; set; }
}