只保存实体中派生类的基类

本文关键字:基类 派生 保存 实体 | 更新日期: 2023-09-27 17:57:53

我试图在实体上下文中只保存派生类实例中的基类,但我得到了错误"无效列名xxx",因为我的表/类的基类没有派生字段,而且它是正确的。有一个表/类来表示派生的它:

class Program
{
    static void Main(string[] args)
    {            
        EntidadeFuncionario func = new EntidadeFuncionario();
        func.TipoPessoa = "F";
        func.Nome = "A";
        func.DataAdmissao = DateTime.Now;
        BLLFuncionario bllFunc = new BLLFuncionario();
        if (bllFunc.Salvar(func))
            Console.Write("Sucesso!");
        else
            Console.Write("Falha");
        Console.ReadLine();
    }
}
public abstract class EntidadePessoa
{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set; }
    public string Nome{get;set;}
    public DateTime? DataNascimento { get; set; }
    public string TipoPessoa { get; set; }
}
public class EntidadeFuncionario : EntidadePessoa
{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set; }
    public DateTime? DataAdmissao { get; set; }

}
 public class BLLFuncionario : BLLPessoaBase
{
    public override bool Salvar(EntidadePessoa func)
    {
        if (base.Salvar((EntidadePessoa)func))
        {                
            if (((EntidadeFuncionario)func).DataAdmissao != DateTime.MinValue)
            {
                Console.WriteLine("func salvo");
                return true;
            }
        }
        return false;
    }
}
public abstract class BLLPessoaBase
{
    ContextWN context = new ContextWN();
    public virtual bool Salvar(EntidadePessoa pessoa)
    {
        if (!string.IsNullOrEmpty(pessoa.Nome))
        {
            context.Pessoas.Add(pessoa);
            context.SaveChanges();
            Console.WriteLine("Pessoa salva");               
            return true;
        }
        return false;
    }
}
public class ContextWN : DbContext
{
    public ContextWN()
        : base("WNEntities")
    {
    }
    public DbSet<EntidadePessoa> Pessoas { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<EntidadePessoa>().ToTable("TB_PESSOA");
    }
}

我有表格TB_PESSOA(Id,Nome,DataNascimento,TipoPessoa)和TB_FUNCIONARIO(Id,DataAdmissao,IdPessoa[FK])

在对象EntidadeFuncionario/Employee中有一个属性(DataAdmissao)。我在BLLPessoaBase类中得到这个对象,就像一个强制转换。到目前为止,好吧,但当我尝试在我的上下文中保存ONLY Person(我现在不想保存EntidadeFuncionario/Employee)(我的上下文没有Entidade Funcionaro dbset,因为它将保留在单独的DLL中)时,c#抛出一个异常"Invalid column DataAdmissao",因为此属性在对象Employees中,即使此对象Employme是强制转换的EntidadeFuncionario/Employment

我的想法是将Pessoa/Person模块保存在一个单独的DLL中,并能够重新实用程序和各种项目,如Funcionario/Emplloyee模块示例。Funcionario/Eemployee模块将在保存"Pessoa/Person"之后进行特定的"保存"。我不知道这是不是一个好的做法,但我这样想

只保存实体中派生类的基类

首先,如果要为实体模型添加更多属性,则应为表中不存在的列使用分部类和[NotMapped]属性。

例如:

public partial class Person
{ 
    public int Id { get; set; }
    public string Nome { get;set;}
    public DateTime? DateTime { get; set; }
}
public partial class Person
{
    [NotMapped]
    public string ExtendProperties { get;set;}
}

在您的情况下,如果您想为各种模块重用实体模型。我建议这样做:

人员模块:

public partial class Person
{ 
     public int Id { get; set; }
     public string Nome { get;set;}
     public DateTime? DateTime { get; set; }
}
public class PersonMapping : EntityTypeConfiguration<Person>
{
     HasKey(x => x.Id);
     ToTable("dbo.TB_PESSOA");     
}

如何添加映射配置

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
      modelBuilder.Configurations.Add(new PersonMapping());
}

员工模块

public partial class Employee : Person
{   
    public string EmployeeCode { get; set; } 
}
public class EmployeeMapping : EntityTypeConfiguration<Employee>
{
     HasKey(x => x.Id);
     ToTable("dbo.TB_EMPLOYEE");     
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Configurations.Add(new PersonMapping());
}

现在,您可以为各种模块重用实体模型。