linq2db如何将一个实体添加到另一个实体的实体列表中

时间:2020-05-19 15:32:39

标签: c# linq2db

linq2db问题 员工有一个技能列表-Skillidfks。 许多员工可以拥有一种技能,一名员工可以具有多种技能。员工和技能由EmployeeSkill实体链接,这也表明该技能的知识水平。 我能否以某种方式将EmployeeSkill实体添加到Skillidfks列表中,以便在保存员工时也保存其EmployeeSkill?

[Table(Schema="public", Name="employee")]
    public partial class Employee
    {
        [Column("id"),                     PrimaryKey,  Identity] public long     Id                   { get; set; } // bigint
        [Column("surname"),                NotNull              ] public string   Surname              { get; set; } // text
        [Column("name"),                   NotNull              ] public string   Name                 { get; set; } // text
        [Column("patronymic"),                Nullable          ] public string   Patronymic           { get; set; } // text
        [Column("phone"),                  NotNull              ] public string   Phone                { get; set; } // text
        [Column("address"),                NotNull              ] public string   Address              { get; set; } // text
        [Column("passport_number_series"), NotNull              ] public string   PassportNumberSeries { get; set; } // text
        [Column("passport_info_whom"),     NotNull              ] public string   PassportInfoWhom     { get; set; } // text
        [Column("passport_info_when"),     NotNull              ] public DateTime PassportInfoWhen     { get; set; } // timestamp (6) without time zone
        [Column("department_id"),             Nullable          ] public long?    DepartmentId         { get; set; } // bigint
        [Column("position_id"),               Nullable          ] public long?    PositionId           { get; set; } // bigint


        [Association(ThisKey="DepartmentId", OtherKey="Id", CanBeNull=true, Relationship=Relationship.ManyToOne, KeyName="employee_fk_1", BackReferenceName="Employeefks")]
        public Department Department { get; set; }

        [Association(ThisKey="PositionId", OtherKey="Id", CanBeNull=true, Relationship=Relationship.ManyToOne, KeyName="employee_fk_2", BackReferenceName="Employeefks")]
        public Position Position { get; set; }

        [Association(ThisKey="Id", OtherKey="EmployeeId", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
        public IEnumerable<EmployeeSkill> Skillidfks { get; set; }

    }
[Table(Schema="public", Name="employee_skill")]
    public partial class EmployeeSkill
    {
        [Column("employee_id"), PrimaryKey(1), NotNull] public long  EmployeeId { get; set; } // bigint
        [Column("skill_id"),    PrimaryKey(2), NotNull] public long  SkillId    { get; set; } // bigint
        [Column("level"),                      NotNull] public short Level      { get; set; } // smallint

        [Association(ThisKey="EmployeeId", OtherKey="Id", CanBeNull=false, Relationship=Relationship.ManyToOne, KeyName="employee_skill_employee_id_fk", BackReferenceName="Skillidfks")]
        public Employee Employee { get; set; }

        [Association(ThisKey="SkillId", OtherKey="Id", CanBeNull=false, Relationship=Relationship.ManyToOne, KeyName="employee_skill_skill_id_fk", BackReferenceName="Employeeidfks")]
        public Skill Skill { get; set; }
    }
[Table(Schema="public", Name="skill")]
    public partial class Skill
    {
        [Column("id"),   PrimaryKey, Identity] public long   Id   { get; set; } // bigint
        [Column("name"), NotNull             ] public string Name { get; set; } // text
        [Association(ThisKey="Id", OtherKey="SkillId", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
        public IEnumerable<EmployeeSkill> Employeeidfks { get; set; }
    }

我尝试了

Employee.Skillidfks.Append(new EmployeeSkill
                        {
                            Employee = Employee, 
                            EmployeeId = Employee.Id,
                            Level = short.Parse(skillLevelChooserViewModel.Level),
                            Skill = Parent.SkillsViewModel.SelectedSkill,
                            SkillId = Parent.SkillsViewModel.SelectedSkill.Id
                        });

但不起作用

0 个答案:

没有答案