在这种情况下,我不理解这种冲突。 我已经检查了可能存在的问题,但现在我无法解决问题。
我希望你能帮助我。如果您需要更多信息,请告诉我。
谢谢,
执行DbCommand失败(32ms)[参数= [@ p0 =' TEST'' (尺寸= 450),@ p1 =' blabla@gmail.com' (大小= 4000),@ p2 =' NULL' (大小= 4000),@ p3 =' CIEP' (大小= 4000), @ P4 =' 0000000000' (大小= 4000)],CommandType ='文字', CommandTimeout =' 30'] SET NOCOUNT ON;插入[联系方式] ([CodeClient],[Email],[GSM],[Nom],[Telephone])VALUES(@ p0,@ p1, @ p2,@ p3,@ p4); SELECT [ContactID] FROM [Contacts] WHERE @@ ROWCOUNT = 1 AND [ContactID] = scope_identity(); System.Data.SqlClient.SqlException(0x80131904):INSERT语句 与FOREIGN KEY约束冲突 " FK_Contacts_Clients_CodeClient&#34 ;.冲突发生在数据库中 " IOR_Presta_DB",table" dbo.Clients",column' CodeClient'。
public class PrestationContext : DbContext
{
public PrestationContext(DbContextOptions<PrestationContext> options)
: base(options)
{
}
public DbSet<Prestation> Prestations { get; set; }
public DbSet<Domaine> Domaines { get; set; }
public DbSet<Employe> Employes { get; set; }
public DbSet<Contrat> Contrats { get; set; }
public DbSet<Client> Clients { get; set; }
public DbSet<Contact> Contacts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder
//Log parameter values
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EmployeDomaine>()
.HasKey(t => new { t.EmployeID, t.DomaineID });
modelBuilder.Entity<EmployeDomaine>()
.HasOne(pt => pt.Employe)
.WithMany(p => p.EmployeDomaines)
.HasForeignKey(pt => pt.EmployeID);
modelBuilder.Entity<EmployeDomaine>()
.HasOne(pt => pt.Domaine)
.WithMany(t => t.EmployeDomaines)
.HasForeignKey(pt => pt.DomaineID);
modelBuilder.Entity<ClientContrat>()
.HasKey(t => new { t.ClientID, t.ContratID });
modelBuilder.Entity<ClientContrat>()
.HasOne(pt => pt.Client)
.WithMany(p => p.ClientContrats)
.HasForeignKey(pt => pt.ClientID);
modelBuilder.Entity<ClientContrat>()
.HasOne(pt => pt.Contrat)
.WithMany(t => t.ClientContrats)
.HasForeignKey(pt => pt.ContratID);
modelBuilder.Entity<Contact>()
.HasOne(c => c.Client)
.WithMany(co => co.Contacts)
.HasForeignKey(co => co.CodeClient)
.HasPrincipalKey(c => c.CodeClient);
}
}
public class Contact
{
public long ContactID { get; set; }
public string Nom { get; set; }
public string Telephone { get; set; }
public string GSM { get; set; }
public string Email { get; set; }
public string CodeClient { get; set; }
public Client Client { get; set; }
}
public class Client
{
public long ClientID { get; set; }
public string CodeClient { get; set; }
public string Nom { get; set; }
public string Adresse { get; set; }
public bool FeedbackClient { get; set; }
public bool EncodagePointage { get; set; }
public ClientContrat[] ClientContrats { get; set; }
public Contact[] Contacts { get; set; }
}
public class DbInitializer
{
public static void Initialize(PrestationContext context)
{
context.Database.EnsureCreated();
if (!context.Clients.Any())
{
var readcsv = File.ReadAllText(@"IorPrestaWebApp\Data\clients.csv");
string[] csvfilerecord = readcsv.Split('\n');
foreach (var row in csvfilerecord)
{
if (!string.IsNullOrEmpty(row))
{
var cells = row.Split(';');
Client client = new Client
{
CodeClient = cells[0],
Nom = cells[1]
};
context.Clients.Add(client);
context.SaveChanges();
}
}
}
if (!context.Contacts.Any())
{
var readcsv = File.ReadAllText(@"IorPrestaWebApp\Data\contacts.csv");
string[] csvfilerecord = readcsv.Split('\n');
foreach (var row in csvfilerecord)
{
if (!string.IsNullOrEmpty(row))
{
var cells = row.Split(';');
Contact contact = new Contact
{
Nom = cells[0],
Telephone = cells[2],
GSM = cells[3],
Email = cells[5],
CodeClient = cells[13]
};
context.Contacts.Add(contact);
context.SaveChanges();
}
}
}
答案 0 :(得分:0)
#Db 上下文#
public class PrestationContext : DbContext
{
public PrestationContext(DbContextOptions<PrestationContext> options)
: base(options)
{
}
public DbSet<Prestation> Prestations { get; set; }
public DbSet<Domaine> Domaines { get; set; }
public DbSet<Employe> Employes { get; set; }
public DbSet<Contrat> Contrats { get; set; }
public DbSet<Client> Clients { get; set; }
public DbSet<Contact> Contacts { get; set; }
}
#Class#
public class Contact
{
public long ContactID { get; set; }
public string Nom { get; set; }
public string Telephone { get; set; }
public string GSM { get; set; }
public string Email { get; set; }
[Display(Name = "Client")]
public string CodeClient { get; set; }
[ForeignKey("CodeClient")]
public Client Client { get; set; }
}
public class Client
{
public long ClientID { get; set; }
public string CodeClient { get; set; }
public string Nom { get; set; }
public string Adresse { get; set; }
public bool FeedbackClient { get; set; }
public bool EncodagePointage { get; set; }
public virtual ICollection<ClientContrat> ClientContrats { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
}