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

Time:12-20

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

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

    }

CodePudding user response:

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

  • Related