Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向现有多对多关系中添加其他列-先添加EF代码_C#_Entity Framework_Many To Many_Entity Framework Migrations_Ef Fluent Api - Fatal编程技术网

C# 向现有多对多关系中添加其他列-先添加EF代码

C# 向现有多对多关系中添加其他列-先添加EF代码,c#,entity-framework,many-to-many,entity-framework-migrations,ef-fluent-api,C#,Entity Framework,Many To Many,Entity Framework Migrations,Ef Fluent Api,我使用的是实体框架6.1.1。我需要向连接表中添加一个额外的字段,这是我用fluentapi正确创建的。我知道这在Fluent-API中是不可能的,我必须创建一个模型类,但问题是我无法将Fluent-API配置映射到实体模型类,而最终不会删除连接表并重新创建它 我有两个模型课:Docente和Lezione public class Docente { public int Id { get; set; } public string Nome { get; set; }

我使用的是实体框架6.1.1。我需要向连接表中添加一个额外的字段,这是我用fluentapi正确创建的。我知道这在Fluent-API中是不可能的,我必须创建一个模型类,但问题是我无法将Fluent-API配置映射到实体模型类,而最终不会删除连接表并重新创建它

我有两个模型课:Docente和Lezione

public class Docente
{
    public int Id { get; set; }
    public string Nome { get; set; }
    [Required]
    public string Cognome { get; set; }
    public virtual ICollection<Lezione> LezioniDocente { get; set; }
}

public class Lezione
{
    public int Id { get; set; }
    public string Programma { get; set; }
    public virtual ICollection<Docente> LezioniDocente { get; set; }
}
我错过什么了吗?如何在不删除现有表的情况下将fluent API关系映射到模型类?
我还尝试在不指定新列的情况下编写LezioneDocente类,但它最终还是会删除表。

我认为,使用fluent API,您无法做任何事情,因为它会告诉entity framework您希望保留现有表,从而生成所需的迁移。但是您可以自己在迁移中更改代码。只需删除所有生成的拖放并创建

您发布的迁移代码似乎与您的模型不匹配。我希望在createtable语句中看到一个
RuoloDocenteId

无论如何,这是我认为您希望在Up方法中使用的代码,而不是您发布的代码:

AddColumn("dbo.LezioneDocente", "RuoloDocenteId", c => c.Int(nullable: false));

嗨,科林,谢谢你的回复。是的,你说得对。我报告的迁移代码是指我尝试在不添加额外字段的情况下编写LezioneDocente类的代码。我根据你的建议修改了迁移代码,结果成功了。令人失望的是,我们无法指定fluent API映射到现有表。
    public class LezioneDocente
    {
        [Key, Column(Order = 0)]
        public int LezioneId { get; set; }

        [Key, Column(Order = 1)]
        public int DocenteId { get; set; }

        public int RuoloDocenteId { get; set; }

        public virtual Docente Docente { get; set; }
        public virtual Lezione Lezione { get; set; }
        public virtual RuoloDocente RuoloDocente { get; set; }
  }

public class Docente
    {
        public int Id { get; set; }
        public string Nome { get; set; }
        [Required]
        public string Cognome { get; set; }
        public virtual ICollection<LezioneDocente> LezioniDocente { get; set; }

    }

public class Lezione
{
        public int Id { get; set; }
        public string Programma { get; set; }
        public virtual ICollection<LezioneDocente> LezioniDocente { get; set; }
}

 public class LezioneDocenteConfiguration : EntityTypeConfiguration<LezioneDocente>
{
    public LezioneDocenteConfiguration()
    {
        HasRequired(_ => _.Lezione)
      .WithMany(_ => _.LezioniDocente)
      .HasForeignKey(_ => _.LezioneId);

        HasRequired(_ => _.Docente)
            .WithMany(_ => _.LezioniDocente)
            .HasForeignKey(_ => _.DocenteId);

        ToTable("LezioneDocente");


    }
}
public partial class test : DbMigration
    {
        public override void Up()
        {
            DropForeignKey("dbo.LezioneDocente", "LezioneId", "dbo.Lezioni");
            DropForeignKey("dbo.LezioneDocente", "DocenteId", "dbo.Docenti");
            DropIndex("dbo.LezioneDocente", new[] { "LezioneId" });
            DropIndex("dbo.LezioneDocente", new[] { "DocenteId" });
            CreateTable(
                "dbo.LezioneDocente",
                c => new
                    {
                        LezioneId = c.Int(nullable: false),
                        DocenteId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.LezioneId, t.DocenteId })
                .ForeignKey("dbo.Docenti", t => t.DocenteId, cascadeDelete: true)
                .ForeignKey("dbo.Lezioni", t => t.LezioneId, cascadeDelete: true)
                .Index(t => t.DocenteId)
                .Index(t => t.LezioneId);

            DropTable("dbo.LezioneDocente");
        }
}
AddColumn("dbo.LezioneDocente", "RuoloDocenteId", c => c.Int(nullable: false));