Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i input text in EF Core through Console.Readline?

I want to be able to enter a name tomy database through Console.Readline();
But I never manage to make it work.
I have tested the following options down below. But I always get an error that there is no object or that it is not possible to convert a string to my model

Any ideas?

My code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 private readonly ApplicationDbContext _db;
        private readonly Customer _customer;

        public AddCustomer(ApplicationDbContext db, Customer customer)
        {
            _db = db;
            _customer = customer;
        }


        public void AddCustomers()
        {


     var CustomerName = _customer.Name.ToString();
 

            Console.WriteLine("Please enter customer name");
            CustomerName = Console.ReadLine();
            _db.Add(CustomerName);



        }

      
    }

My model

public class Customer
    {
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }

        public string CustomerDog { get; set; }

        public string CustomerDogBreed { get; set; }

        public int CustomerDogSocialNumber { get; set; }

        public Services services { get; set; }

    }

>Solution :

As said in my comment. You’re trying adding CustomerName to the db but I seriously doubt any table in the db is a string table.

To add a new customer to the customer table you’d need to create a new customer object, set the Name to the customer name and add it.

Please see this: Insert data using Entity Framework model

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading