J'essaie de cretae constrint entre deux tables en utilisant le code première attitude:Pourquoi ai-je une erreur sur la commande update-database?
est ici entités:
public class Client : IEntity
{
public int Id { get; set; }
public int IdentityNumber { get; set; }
public int? ClientTypeId { get; set; }
public string ClientName { get; set; }
public string Departament { get; set; }
public string Division { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
[ForeignKey("ClientTypeId")]
public virtual ClientType ClientType { get; set; }
}
Deuxième entité:
public class ClientType : ILookupEntity
{
public int Id { get; set; }
public string Description { get; set; }
public string Comment { get; set; }
}
Voici créé la migration:
public override void Up()
{
CreateTable(
"dbo.ClientTypes",
c => new
{
Id = c.Int(nullable: false, identity: true),
Description = c.String(),
Comment = c.String(),
})
.PrimaryKey(t => t.Id);
CreateIndex("dbo.Clients", "ClientTypeId");
AddForeignKey("dbo.Clients", "ClientTypeId", "dbo.ClientTypes", "Id");
}
public override void Down()
{
DropForeignKey("dbo.Clients", "ClientTypeId", "dbo.ClientTypes");
DropIndex("dbo.Clients", new[] { "ClientTypeId" });
DropTable("dbo.ClientTypes");
}
}
Mais sur la commande update-database j'obtiens ceci erreur:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Clients_dbo.ClientTypes_ClientTypeId". The conflict occurred in database "Playground", table "dbo.ClientTypes", column 'Id'.
Tout ce qui peut causer l'erreur?