添加额外的列以加入 table
Adding Extra Column to join table
我目前有一个员工模型
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<StateLicenseType> Licenses { get; set; }
和许可证类型模型
public class StateLicenseType
{
public int StateLicenseTypeId { get; set; }
public string State { get; set; }
public string LicenseName { get; set; }
public virtual Employee Employee { get; set; }
}
这个关系可以是一对多的,但是我在保存的时候还需要在license里面添加一些信息。我需要能够存储员工的唯一许可证号,但在四处搜索时无法找到如何执行此操作。有没有办法让 Entity Framework 向联接 table 添加一列,然后即使我必须这样做,也可以自己更新它?
是否有better/different方法来模拟这种与 EF 的关系?
在旧数据库中,table 是这样创建的,
CREATE TABLE `nmlsstatelicenses` ( `peopleid` int(11) DEFAULT NULL, `statelicensetypeid` int(11) DEFAULT NULL, `licensenumber` varchar(25) DEFAULT NULL)
您需要创建第三个实体,它将成为链接实体(如数据库中多对多关系中的链接 table。这是一个示例:many-to-many relationships with additional information.
因此您的模型中将包含以下实体:
public Employee
{
public string EmployeeId { get;set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<LicenseRegistration> RegisteredLicenses { get; set; }
}
public LicenseType
{
public int StateLicenseTypeId { get; set; }
public string State { get; set; }
public string LicenseName { get; set; }
public virtual ICollection<LicenseRegistration> RegisteredLicenses { get; set; }
}
public LicenseRegistration
{
//properties for the additional information go here
/////////////////////////////////////////////////////
public int EmployeeId {get;set;}
[ForeignKey("EmployeeId")]
public Employee Employee {get;set;}
public int LicenseTypeId {get;set;}
[ForeignKey("LicenseTypeId")]
public LicenseType {get;set;}
}
然后,在您的 DBContext 文件中,您需要定义 Employee 和 LicenseRegistration 之间以及 LicenseType 和 LicenseRegistration 之间的一对多关系。
希望对您有所帮助!
更新
以下是建立关系的方式:
modelbuilder.Entity<LicenseRegistration>()
.HasRequired(lr => lr.LicenseType)
.WithMany(lt => lt.RegisteredLicenses)
.HasForeignKey(lr => lr.LicenseTypeId);
modelbuilder.Entity<LicenseRegistration>()
.HasRequired(lr => lr.Employee)
.WithMany(e => e.RegisteredLicenses)
.HasForeignKey(lr => lr.EmployeeId);
我目前有一个员工模型
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<StateLicenseType> Licenses { get; set; }
和许可证类型模型
public class StateLicenseType
{
public int StateLicenseTypeId { get; set; }
public string State { get; set; }
public string LicenseName { get; set; }
public virtual Employee Employee { get; set; }
}
这个关系可以是一对多的,但是我在保存的时候还需要在license里面添加一些信息。我需要能够存储员工的唯一许可证号,但在四处搜索时无法找到如何执行此操作。有没有办法让 Entity Framework 向联接 table 添加一列,然后即使我必须这样做,也可以自己更新它?
是否有better/different方法来模拟这种与 EF 的关系?
在旧数据库中,table 是这样创建的,
CREATE TABLE `nmlsstatelicenses` ( `peopleid` int(11) DEFAULT NULL, `statelicensetypeid` int(11) DEFAULT NULL, `licensenumber` varchar(25) DEFAULT NULL)
您需要创建第三个实体,它将成为链接实体(如数据库中多对多关系中的链接 table。这是一个示例:many-to-many relationships with additional information.
因此您的模型中将包含以下实体:
public Employee
{
public string EmployeeId { get;set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<LicenseRegistration> RegisteredLicenses { get; set; }
}
public LicenseType
{
public int StateLicenseTypeId { get; set; }
public string State { get; set; }
public string LicenseName { get; set; }
public virtual ICollection<LicenseRegistration> RegisteredLicenses { get; set; }
}
public LicenseRegistration
{
//properties for the additional information go here
/////////////////////////////////////////////////////
public int EmployeeId {get;set;}
[ForeignKey("EmployeeId")]
public Employee Employee {get;set;}
public int LicenseTypeId {get;set;}
[ForeignKey("LicenseTypeId")]
public LicenseType {get;set;}
}
然后,在您的 DBContext 文件中,您需要定义 Employee 和 LicenseRegistration 之间以及 LicenseType 和 LicenseRegistration 之间的一对多关系。
希望对您有所帮助!
更新 以下是建立关系的方式:
modelbuilder.Entity<LicenseRegistration>()
.HasRequired(lr => lr.LicenseType)
.WithMany(lt => lt.RegisteredLicenses)
.HasForeignKey(lr => lr.LicenseTypeId);
modelbuilder.Entity<LicenseRegistration>()
.HasRequired(lr => lr.Employee)
.WithMany(e => e.RegisteredLicenses)
.HasForeignKey(lr => lr.EmployeeId);