A
A
authoraytee2020-08-25 11:28:26
C++ / C#
authoraytee, 2020-08-25 11:28:26

How to update MySQL database, C#?

I connected the database, created a context, models, then I write enable-migrations, add-migration - everything is fine
I enter update-database - it gives an error "The input string had the wrong format", how to fix it?...
(When starting the project, the same error is generated )

static void Main(string[] args)
        {
            DatabaseContext context = new DatabaseContext();
                
            var group = new Group()
            {
                Name = "Gruppa1",
                Year = 2002,
            };

            //При запуске проекта та же самая ошибка в этом месте
            context.Groups.Add(group);
            context.SaveChanges();

            Console.WriteLine("Группа = ", group.Name);
            Console.ReadKey();
        }


Connected like this:
<connectionStrings>
    <add name="conn" connectionString="Database=newdb;Data Source=localhost;User Id=root;Password=password1122;Integrated Security=True" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>


Context:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]

    public class DatabaseContext : DbContext
    {
        public DatabaseContext() : base("conn")
        {
        }

        public DbSet<Group> Groups { get; set; }
        public DbSet<Song> Songs { get; set; }
    }


Models:
public class Group
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Year { get; set; }

        public virtual ICollection<Song> Songs { get; set; } 
    }

    public class Song
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int GroupId { get; set; }

        public virtual Group Group { get; set; } 
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-08-25
@vabka

> base("conn")
Is this supposed to work?
Most likely you need to do something like config.GetConnectionString("conn")
What runtime is used?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question