0

我有以下 DbContext、模型和控制器(首先是数据库)。这些是两个表(operador 和operador2)的参照物,它们相互补充。我成功地将这些模型(表“operador”)映射到(某些表),但是(表“operador”)和(表“operador2”)的映射参考不能正常工作。我想我没有什么可以正常工作的。(在控制器执行后的代码和图像下方。)我需要属性“OpedradorId”和“Operador2Id”之间的关系。有人可以帮助我吗?

/**** DbContext ****/
public class OperadorContext : DbContext
{
    public OperadorContext() : base("name=CnxEasyLabWeb") { }

    public DbSet<OperadorModel> DbOperador { get; set; }
    public DbSet<OpCadastros1Model> DbCadastro { get; set; }
    public DbSet<OpCadastros2Model> DbCadastro2 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<OperadorModel>().HasKey(o => o.OperadorId);

        modelBuilder.Entity<OpCadastros1Model>().HasKey(c => c.OperadorId);

        modelBuilder.Entity<OpCadastros1Model>()
            .HasRequired(c => c.Operador)
            .WithRequiredPrincipal(o => o.OpCadastros1);

        modelBuilder.Entity<OpCadastros1Model>()
            .HasRequired(c => c.OpCadastros2)
            .WithRequiredDependent(d => d.OpCadastro1);

        modelBuilder.Entity<OperadorModel>().ToTable("operador", "public");
        modelBuilder.Entity<OpCadastros1Model>().ToTable("operador", "public");
        modelBuilder.Entity<OpCadastros2Model>().ToTable("operador2", "public");
    }
}

/**** Models ****/
 public class OpCadastros1Model //: OpGeralModel
{
    [Key, Column("id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OperadorId { get; set; }

    public virtual OperadorModel Operador { get; set; }

    public virtual OpCadastros2Model OpCadastros2 { get; set; }
}

public class OpCadastros2Model
{
    [Key, Column("operador_id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OperadorId { get; set; }

    public virtual OpCadastros1Model OpCadastro1 { get; set; }
}

public class OperadorModel
{
    [Key, Column("id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OperadorId { get; set; }

    public virtual OpCadastros1Model OpCadastros1 { get; set; }

    [Column("nome")]
    [Required]
    public string Login { get; set; }
}

/**** Controller ****/
[HttpPost]
//[Authorize]
[ValidateAntiForgeryToken]
public ContentResult SalvarCadastros(OpCadastros1Model model)
{
    if (ModelState.IsValid)
    {
        #region Update
        if (model.OperadorId > 0)
        {
            var update = _db.DbCadastro.Single(op => op.OperadorId == model.OperadorId);
            _db.Entry(update).CurrentValues.SetValues(model);
            _db.Entry(update).State = EntityState.Modified;         
        }
        #endregion

        #region New
        else
        {
            _db.DbCadastro.Add(model);
        }
        #endregion

        _db.SaveChanges();
    }
    else
    { ... }

    ...
}

传递给控制器​​的图像模型对于 OpCadastro2 为空

作为补充,我收到此错误:

属性“Operador2Id”是对象的关键信息的一部分,不能修改。

4

1 回答 1

0

您必须有一个类是主要的,而其他类是细节。在主节点上,正常声明所有内容:

public class Operador
{
    [Key, Column("id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OperadorId { get; set; }
    public virtual OpCadastros1Model OpCadastros1 { get; set; }
}

在辅助上,执行以下操作:

public class OpCadastros1Model 
{
    [Key, Column("id"), ForeignKey("Operador")]
    public int OperadorId { get; set; }
    [Required]
    public Operador Operador { get; set; }
}

这意味着主节点与细节有一个可选的关系,而细节与主节点有一个必需的关系并从中获取其主键。

于 2013-09-03T03:51:49.153 回答