C# 如何更新与EF映射的一对多关系中的外键?
我使用EF约定创建了一对多关系。然后在C# 如何更新与EF映射的一对多关系中的外键?,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我使用EF约定创建了一对多关系。然后在updatedatabase之后,我在WorkRoles表中得到了一个Company\u CompanyId列,这是期望的结果 但是,当我试图实际使用这个外键,以便将不同的角色与不同的公司联系起来时,我陷入了困境 在我的WorkRoleController中,我希望能够以访问其他属性的方式访问FK属性,但事实并非如此 当我键入workrole(dot)时,我看到的是RoleName,RoleDescription,WorkRoleId,而不是Company\
updatedatabase
之后,我在WorkRoles
表中得到了一个Company\u CompanyId
列,这是期望的结果
但是,当我试图实际使用这个外键,以便将不同的角色与不同的公司联系起来时,我陷入了困境
在我的WorkRoleController
中,我希望能够以访问其他属性的方式访问FK属性,但事实并非如此
当我键入workrole(dot)
时,我看到的是RoleName
,RoleDescription
,WorkRoleId
,而不是Company\u CompanyId
,我看到的只是Company
。这是对象公司
,而不是外键
所以我的问题是,如何使用外键,所以在创建新角色时,我可以执行以下操作:
Company company = db.Companies.Where(c => c.CAId == currentUserId)
.FirstOrDefault();
int companyID = company.CompanyId; // find the company for the current user
workRole.Company_CompanyId = companyID; // save the company ID to the FK field.
有人能帮忙吗?保存工作角色时如何实际保存外键
榜样:
public class WorkRole
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WorkRoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public virtual Company Company { get; set; }
}
公司模式:
public class Company
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CompanyId { get; set; }
public byte[] ImageData { get; set; }
[NotMapped]
public HttpPostedFileBase UploadImage { get; set; }
[NotMapped]
public string ImageBase64 => System.Convert.ToBase64String(ImageData);
public string CompanyName { get; set; }
public string CAId { get; set; }
public virtual ICollection<WorkRole> WorkRoles { get; set; }
}
上市公司
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
public int CompanyId{get;set;}
公共字节[]图像数据{get;set;}
[未映射]
公共HttpPostedFileBase上载映像{get;set;}
[未映射]
公共字符串ImageBase64=>System.Convert.ToBase64String(ImageData);
公共字符串CompanyName{get;set;}
公共字符串CAId{get;set;}
公共虚拟ICollection工作角色{get;set;}
}
将CompanyId
属性添加到WorkRole类中,将更改推送到数据库中,然后在键入WorkRole(dot)时应该会看到它,并根据需要进行更新
public class WorkRole
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WorkRoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public int CompanyId {get; set;}// this will be mapped as foreign key by EF convention.
public virtual Company Company { get; set; }
}
如果您的外键属性名称不符合任何EF约定,您可以使用[ForeignKey]
数据注释让EF知道您刚才定义的属性实际上是外键:
public class WorkRole
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WorkRoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
[ForeignKey("Company")]
public int Foo {get; set;}
public virtual Company Company { get; set; }
}
您还可以在导航属性上使用它:
public class WorkRole
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WorkRoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public int Foo {get; set;}
[ForeignKey("Foo")]
public virtual Company Company { get; set; }
}
将CompanyId
属性添加到WorkRole类中,将更改推送到数据库中,然后在键入WorkRole(dot)时应该会看到它,并根据需要进行更新
public class WorkRole
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WorkRoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public int CompanyId {get; set;}// this will be mapped as foreign key by EF convention.
public virtual Company Company { get; set; }
}
如果您的外键属性名称不符合任何EF约定,您可以使用[ForeignKey]
数据注释让EF知道您刚才定义的属性实际上是外键:
public class WorkRole
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WorkRoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
[ForeignKey("Company")]
public int Foo {get; set;}
public virtual Company Company { get; set; }
}
您还可以在导航属性上使用它:
public class WorkRole
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WorkRoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public int Foo {get; set;}
[ForeignKey("Foo")]
public virtual Company Company { get; set; }
}
但是我应该如何处理公共虚拟公司Company{get;set;}呢?您可能想研究外键关联与独立关联。您认为我应该使用外键属性,还是只使用公共int CompanyId{get;set;}就可以了,@GertArnoldokay,谢谢,我将对此进行研究,但是我应该如何处理公共虚拟公司Company{get;set;}呢?您可能想研究外键关联与独立关联。您认为我应该使用外键属性,还是只使用公共int CompanyId{get;set;}就可以了,@GertArnoldokay,谢谢,我会研究的