EFcore2:我为数据库

时间:2018-02-27 08:54:56

标签: .net entity-framework asp.net-core

在这种情况下,我不理解这种冲突。 我已经检查了可能存在的问题,但现在我无法解决问题。

  1. 这是一对多关系,表格"联系"必须加入表"客户"在密钥代码客户端上。客户有很多联系人,联系人有一个客户。
  2. 之前插入CSV中的客户端数据 联系人数据和密钥存在于客户端表中。
  3. 当我在应用程序的启动时播种数据库时,会出现此问题。 如果我直接在SQL Server管理工作室中插入相同的数据则没有 问题。
  4. 我认为这不是数据" order"问题,但可能是我这样做的方式。
  5. 我希望你能帮助我。如果您需要更多信息,请告诉我。

    谢谢,

    错误:

      

    执行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'。

    我的DbContext:

    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();
                    }
                }
            }
    

1 个答案:

答案 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; }
}