Saving data in asp.net core 3.1 Entity error, it adds fields

0

I have at issue at saving a model in the database. I got the following Model:

[Table("data_details")]
    public class DataDetail
    {
        [Key]
        [Display(Name = "ID")]
        [Column("id")]
        public String Id { get; set; }

        [Display(Name = "QUALIFICATION")]
        [Column("qualification")]
        public decimal? Qualification { get; set; }

        [Display(Name = "DATA CONFIG ID")]
        [Column("data_config_id")]
        public String DataConfigId { get; set; } 
        public DataConfig DataConfig { get; set; }
    }

In my dbcontext I have it like this:

    modelBuilder.Entity<DataDetail>().HasOne(c => c.Dataconfig).WithMany(w => w.DataDetails).HasForeignKey(c => c.DataConfigId);

    modelBuilder.Entity<DataDetail>().HasOne(c => c.Data).WithMany(w => w.DataDetails).HasForeignKey(c => c.DataId);

When Saving

 _context.Add(dataDetail);
_context.SaveChanges();

It tries to save but in the log I get an error , like it's trying to add an extra field

INSERT INTO `data_details` (`id`, `qualification`,  `data_config_id`, `DataId`)
      VALUES (@p0, @p1, @p2, @p3);
Microsoft.EntityFrameworkCore.Update[10000]
      An exception occurred in the database while saving changes for context type 'Systema.Data.SystemaContext'.
      Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
       ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'DataId' in 'field list'

DataId Is not specified in this table at all :S

asp.net
asp.net-core
entity
asked on Stack Overflow Apr 28, 2021 by Franco Gutierrez • edited Apr 29, 2021 by Yinqiu

1 Answer

0

In your dbcontext,you have created a many-to-many relationship between Dataconfig and Data.EFCore will create DataConfigId and DataId as foreign keys of DataDetail by default.

Add DataDetail means to create a relationship between Dataconfig and Data. The DataId and DataconfigId you add must be existing entities, otherwise an error will be reported.

Your code should be:

 public class Data
{
    public string Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<DataDetail> DataDetails { get; set; }
}
public class DataDetail
{
    [Key]
    [Display(Name = "ID")]
    [Column("id")]
    public String Id { get; set; }

    [Display(Name = "QUALIFICATION")]
    [Column("qualification")]
    public decimal? Qualification { get; set; }

    [Display(Name = "DATA CONFIG ID")]
    [Column("data_config_id")]
    public string DataConfigId { get; set; }
    public DataConfig DataConfig { get; set; }

    [Display(Name = "DATA Data ID")]
    [Column("data_id")]
    public string DataId { get; set; }
    public Data Data { get; set; }
}
public class DataConfig
{
    public string Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<DataDetail> DataDetails {get;set;}
}

About many to many releationship.

answered on Stack Overflow Apr 29, 2021 by Yinqiu • edited Apr 29, 2021 by Yinqiu

User contributions licensed under CC BY-SA 3.0